-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionVariable.cpp
More file actions
50 lines (39 loc) · 1.3 KB
/
conditionVariable.cpp
File metadata and controls
50 lines (39 loc) · 1.3 KB
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
44
45
46
47
48
49
50
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
std::mutex gLock;
std::condition_variable gConditionVariable;
int main()
{
int result = 0;
bool notified = false; // to notify the work has been done by working thread
// reporter thread
std::thread reporter([&]
{
std::unique_lock<std::mutex> lock(gLock);
std::cout << "Waiting to work to be done."<< std::endl;
if(!notified){
gConditionVariable.wait(lock);
}
std::cout << "Reporter, result is: " << result << std::endl; });
// worker thread
std::thread worker([&]
{
std::unique_lock<std::mutex> lock(gLock);
// got the lock, do the work
result = 42 + 1 + 7;
// work is completed
notified = true;
std::cout << "Work complete, waiting 5 seconds."<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "Wait time is up." << std::endl;
// wake the waiting thread
gConditionVariable.notify_one(); });
// join threads before program ends
reporter.join();
worker.join();
std::cout << "Program completed!" << std::endl;
return 0;
}