forked from mpavezb/cpp_concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_condition_variable.cpp
43 lines (35 loc) · 1005 Bytes
/
01_condition_variable.cpp
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
35
36
37
38
39
40
41
42
43
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
int total_distance = 5;
int distance_covered = 0;
std::condition_variable cv;
std::mutex mutex;
void driver() {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
distance_covered++;
printf("Driver: Moved 1m forward. distance_covered=%d\n", distance_covered);
if (distance_covered == total_distance) {
printf("Driver: Arrived!, Will notify him.\n");
cv.notify_one();
break;
}
}
}
void passenger() {
std::unique_lock<std::mutex> lock(mutex);
// This predicated overload takes care of everything.
cv.wait(lock, [] { return distance_covered == total_distance; });
printf("Passenger: I am there!, distance_covered=%d\n", distance_covered);
}
int main() {
std::thread driver_thread(driver);
std::thread passener_thread(passenger);
passener_thread.join();
driver_thread.join();
}