-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcondition_variable.cpp
More file actions
34 lines (28 loc) · 898 Bytes
/
condition_variable.cpp
File metadata and controls
34 lines (28 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Demonstrates §4.4 of docs/multithreading.md: worker notifies main using a
// condition variable, with the inner-scope notify pattern that avoids the
// wake-bounce.
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
std::mutex mu;
std::condition_variable cv;
bool done = false;
void worker() {
// Pretend to do work.
std::this_thread::sleep_for(std::chrono::milliseconds(200));
// Inner scope: release the lock *before* notify_one, so the woken
// waiter can take the lock immediately (avoids the wake-bounce).
{
std::scoped_lock lock(mu);
done = true;
}
cv.notify_one();
}
int main() {
std::jthread t(worker);
std::unique_lock<std::mutex> lock(mu);
cv.wait(lock, [] { return done; }); // predicate form: re-checks on every wake
std::cout << "worker finished, done = " << std::boolalpha << done << '\n';
}