AtomicCondition: add helpers set_and_notify_{one,all}

This commit is contained in:
Tom 2020-07-17 16:50:51 +02:00
parent e69a62cf9d
commit 346c835468
2 changed files with 35 additions and 0 deletions

View File

@ -42,6 +42,18 @@ public:
this->value_.store(val); 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(); } auto native_handle() { return this->condvar_.native_handle(); }
void notify_one() const noexcept { this->condvar_.notify_one(); } void notify_one() const noexcept { this->condvar_.notify_one(); }
void notify_all() const noexcept { this->condvar_.notify_all(); } void notify_all() const noexcept { this->condvar_.notify_all(); }

View File

@ -49,6 +49,10 @@ TEST_CASE("condition-get-set")
REQUIRE( condition.get() == 23 ); REQUIRE( condition.get() == 23 );
condition.set(42); condition.set(42);
REQUIRE( condition.get() == 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") TEST_CASE("wait")
@ -101,6 +105,25 @@ TEST_CASE("wait-for-predicate")
REQUIRE( condition.get() == 42 ); REQUIRE( condition.get() == 42 );
} }
TEST_CASE("wait-for-predicate-simple")
{
sgnl::AtomicCondition condition(23);
auto pred = [&condition](){ return condition.get() == 42; };
std::future<void> 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") TEST_CASE("wait-until-predicate")
{ {
sgnl::AtomicCondition condition(23); sgnl::AtomicCondition condition(23);