-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cc
56 lines (45 loc) · 1.33 KB
/
main.cc
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
#include <cstdlib>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
#include <chrono>
std::mutex g_mut;
std::condition_variable g_cond;
bool g_ready = false;
void loop()
{
std::unique_lock<std::mutex> lck(g_mut);
g_cond.wait(lck, []() {
return g_ready; // условие, когда прекратить ожидать.
});
std::cout << "Hello, World!" << std::endl;
}
void test1()
{
std::thread thd(&loop);
thd.join();
// Зависание...
}
void test2()
{
g_ready = true;
std::thread thd(&loop);
thd.join();
// Зависания не будет потому что сначала проверяется условие.
}
void test3()
{
std::thread thd(&loop);
// Без задержки g_ready = true; успеет выполниться еще того как начнет работать loop()
std::this_thread::sleep_for(std::chrono::seconds(1)); // https://stackoverflow.com/a/10613664/12847278
g_ready = true;
g_cond.notify_one(); // Без этой строки будет зависание...
thd.join();
}
int main(int argc, char* argv[])
{
test2();
system("read -n 1 -s -p \"Press any key to continue...\"; echo"); // https://discussions.apple.com/thread/2130719
return EXIT_SUCCESS;
}