diff --git a/include/sgnl/AtomicCondition.h b/include/sgnl/AtomicCondition.h index 0b8163d..07fd8e3 100644 --- a/include/sgnl/AtomicCondition.h +++ b/include/sgnl/AtomicCondition.h @@ -42,6 +42,18 @@ public: this->value_.store(val); } + void set_and_notify_one(ValueType val) noexcept + { + this->set(std::move(val)); + this->condvar_.notify_one(); + } + + void set_and_notify_all(ValueType val) noexcept + { + this->set(std::move(val)); + this->condvar_.notify_all(); + } + auto native_handle() { return this->condvar_.native_handle(); } void notify_one() const noexcept { this->condvar_.notify_one(); } void notify_all() const noexcept { this->condvar_.notify_all(); } diff --git a/test/test.cpp b/test/test.cpp index 4f71349..5ac5ada 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -49,6 +49,10 @@ TEST_CASE("condition-get-set") REQUIRE( condition.get() == 23 ); condition.set(42); REQUIRE( condition.get() == 42 ); + condition.set_and_notify_all(1); + REQUIRE( condition.get() == 1 ); + condition.set_and_notify_one(2); + REQUIRE( condition.get() == 2 ); } TEST_CASE("wait") @@ -101,6 +105,25 @@ TEST_CASE("wait-for-predicate") REQUIRE( condition.get() == 42 ); } +TEST_CASE("wait-for-predicate-simple") +{ + sgnl::AtomicCondition condition(23); + auto pred = [&condition](){ return condition.get() == 42; }; + std::future future = + std::async( + std::launch::async, + [&condition, &pred](){ + condition.wait_for(std::chrono::hours(1000), pred); }); + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::yield(); + + condition.set_and_notify_all(42); + future.wait(); + + REQUIRE( condition.get() == 42 ); +} + TEST_CASE("wait-until-predicate") { sgnl::AtomicCondition condition(23);