...
Code Block |
---|
#include <condition_variable>
#include <mutex>
extern bool until_finish(void);
extern std::mutex m;
extern std::condition_variable condition;
void func(void) {
std::unique_lock<std::mutex> lk(m);
while (until_finish()) { // Predicate does not hold.
condition.wait(lk);
}
// Resume when condition holds.
// . . .
} |
The notification mechanism notifies the waiting thread and allows it to check its condition predicate. The invocation of notify_all()
in another thread cannot precisely determine which waiting thread will be resumed. Condition predicate statements allow notified threads to determine whether they should resume upon receiving the notification.
...