/
main.cpp
75 lines (52 loc) · 1.21 KB
/
main.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
#include <atomic>
#include <vector>
#include <chrono>
#include <thread>
#include <mutex>
#include <string>
#include <iostream>
#include <chrono>
using namespace std;
void thread_func(atomic_bool& stop, atomic_bool &thread_done, vector<string>& vs, mutex& m)
{
thread_done = false;
while (!stop)
{
m.lock();
vs.push_back("test");
m.unlock();
}
thread_done = true;
}
int main(void)
{
atomic_bool stop = false;
atomic_bool thread_done = false;
mutex m;
vector<string> vs;
thread t(thread_func, ref(stop), ref(thread_done), ref(vs), ref(m));
auto start_time = std::chrono::system_clock::now();
auto end_time = start_time + std::chrono::seconds(2);
while (!stop)
{
auto curr_time = std::chrono::system_clock::now();
if (curr_time >= end_time)
stop = true;
cout << "Printing log:" << endl;
m.lock();
for (vector<string>::const_iterator ci = vs.begin(); ci != vs.end(); ci++)
cout << *ci << endl;
vs.clear();
cout << endl;
m.unlock();
}
while (!thread_done)
{
cout << "Waiting for thread" << endl;
// draw graphics
}
cout << "Joining" << endl;
t.join();
cout << "Done" << endl;
return 0;
}