-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConditionVariable.cpp
113 lines (104 loc) · 2.64 KB
/
ConditionVariable.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <list>
#include <string>
#include <atomic>
using namespace std::chrono_literals;
namespace chrono = std::chrono;
// a simple implementation of std::this_thread::sleep_for
template<typename Rep, typename Period>
void my_sleep_for(const chrono::duration<Rep, Period>& d)
{
std::condition_variable timer;
std::mutex mtx;
auto t0 = chrono::steady_clock::now();
std::unique_lock<std::mutex> lck(mtx);
timer.wait_for(lck, d);
auto t1 = chrono::steady_clock::now();
std::cout << chrono::duration_cast<chrono::milliseconds>(t1-t0) << " passed" << std::endl;
}
// reimplement consumer/producer
template<typename T>
class SyncQueue
{
public:
void put(const T& val)
{
std::lock_guard lck(mtx);
q.push_back(val);
cond.notify_one();
}
void put(T&& val)
{
std::lock_guard lck(mtx);
q.push_back(std::move(val));
cond.notify_one();
}
bool get(T& val) // return success or not
{
std::unique_lock lck(mtx);
bool res = cond.wait_for(lck, 1ms, [this]() { return !q.empty(); });
if (!res)
{
return false;
}
val = q.front();
q.pop_front();
return true;
}
private:
std::mutex mtx;
std::condition_variable cond;
std::list<T> q;
};
using Message = std::string;
SyncQueue<Message> mq;
std::atomic_flag end_of_produce { ATOMIC_FLAG_INIT };
std::mutex mcout; // for cout
void producer()
{
for (int i = 0; i < 200; ++i)
{
{
std::lock_guard lck(mcout);
std::cout << "producer("<< std::this_thread::get_id() << "): " << i << std::endl;
}
mq.put(std::to_string(i));
// std::this_thread::sleep_for(3ms);
}
end_of_produce.test_and_set();
}
void consumer()
{
while (true)
{
Message message;
bool res = mq.get(message);
// end of consuming
if (!res && end_of_produce.test())
{
std::lock_guard lck(mcout);
std::cout << "\t\t\tend of consuming(" << std::this_thread::get_id() << ")" << std::endl;
return;
}
// consuming
if (res)
{
std::lock_guard lck(mcout);
std::cout << "\t\t\tconsumer("<< std::this_thread::get_id() << "): " << message << std::endl;
}
}
}
int main(int argc, char const *argv[])
{
my_sleep_for(100ms);
std::jthread p(producer);
std::jthread c1(consumer);
std::jthread c2(consumer);
std::jthread c3(consumer);
std::jthread c4(consumer);
return 0;
}