AtomicCondition: add helpers set_and_notify_{one,all}
This commit is contained in:
parent
e69a62cf9d
commit
346c835468
@ -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(); }
|
||||
|
@ -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<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")
|
||||
{
|
||||
sgnl::AtomicCondition condition(23);
|
||||
|
Reference in New Issue
Block a user