-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
92 lines (82 loc) · 2.3 KB
/
test.cpp
File metadata and controls
92 lines (82 loc) · 2.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
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
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <thread>
#include <time.h>
// template <typename T>
// T returnWhenTrue(T val, bool flag)
// {
// if (flag)
// return val;
// else
// return 0;
// }
bool KILL_THREAD = false;
void wait2SecondsPrintHi()
{
while (true) {
if (KILL_THREAD) {
return;
}
std::cout << "hi" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
}
int main()
{
std::thread t1 = std::thread(wait2SecondsPrintHi);
std::this_thread::sleep_for(std::chrono::milliseconds(8000));
std::cout << t1.joinable() << std::endl;
KILL_THREAD = true;
std::cout << "killed thread once." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout << t1.joinable() << std::endl;
KILL_THREAD = true;
std::cout << "killed thread twice." << std::endl;
t1.join();
return 0;
// returnWhenTrue(1, true);
// returnWhenTrue(1, false);
// return 0;
//
// std::map<int, std::map<std::string, int>> mps;
// mps[0]["zero"] = 0;
// mps[0]["one"] = 1;
// mps[0]["two"] = 2;
// mps[1]["zero"] = 1;
// mps[1]["one"] = 2;
// mps[2]["two"] = 4;
// for (auto [str, num] : mps[0]) {
// std::cout << str << ' ' << num << std::endl;
// }
// return 0;
//
// srand(time(NULL));
// for (int i = 0; i < 10; ++i) {
// std::cout << rand() << std::endl;
// }
// return 0;
//
// time_t t1 = time(NULL);
// std::this_thread::sleep_for(std::chrono::milliseconds(5000));
// time_t t2 = time(NULL);
// std::cout << t1 << ' ' << t2 << std::endl;
// return 0;
//
// std::set<int> test_set;
//
// test_set.insert(1);
// auto fit = test_set.find(1);
// std::cout << (fit == test_set.end()) << std::endl;
// int cnt = test_set.erase(1);
// std::cout << cnt << std::endl;
//
// test_set.insert(1);
// fit = test_set.find(2);
// std::cout << (fit == test_set.end()) << std::endl;
// cnt = test_set.erase(2);
// std::cout << cnt << std::endl;
//
// return 0;
}