forked from jakaspeh/concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditionVariable2.cpp
More file actions
88 lines (70 loc) · 1.66 KB
/
conditionVariable2.cpp
File metadata and controls
88 lines (70 loc) · 1.66 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <queue>
#include <random>
#include <chrono>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex MUT;
std::queue <std::string> TASKS;
std::condition_variable CV;
int SEED = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine GENERATOR(SEED);
std::uniform_int_distribution<int> DISTRIBUTION(0, 4);
std::vector<std::string> TODO = {"Write program",
"Fix bug",
"Write unit tests",
"Bring me coffee",
"Clean my car"};
void add_task(const std::string& task)
{
std::lock_guard<std::mutex> guard(MUT);
std::cout << "Boss delegates: " << task << std::endl;
TASKS.push(task);
CV.notify_one();
}
void add_random_task()
{
add_task(TODO[DISTRIBUTION(GENERATOR)]);
}
void go_home()
{
add_task("go home");
}
void take_break()
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
void boss()
{
for (int i = 0; i != 5; i++)
{
add_random_task();
take_break();
}
go_home();
}
void worker()
{
while (true)
{
std::unique_lock<std::mutex> guard(MUT);
CV.wait(guard, []{return !TASKS.empty();});
std::string task = TASKS.front();
TASKS.pop();
std::cout << "Worker doing: " << task << std::endl;
if (task == "go home")
{
break;
}
}
}
int main()
{
std::thread t1(boss);
std::thread t2(worker);
t1.join();
t2.join();
return 0;
}