Skip to content

Commit 944e1f5

Browse files
committed
thread usage
Signed-off-by: ericyuanhui <ericxu890302@gmail.com>
1 parent 27bd3dc commit 944e1f5

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

Language/C_plus/thread/thread.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//g++ thread.cpp -pthread/-lpthread -std=c++11
2+
3+
#include <iostream> // std::cout
4+
#include <thread> // std::thread, std::this_thread::sleep_for
5+
#include <chrono> // std::chrono::seconds
6+
#include <array>
7+
#include <mutex>
8+
#include <atomic>
9+
10+
using namespace std;
11+
12+
const int N = 1000000000;
13+
int num = 0;
14+
mutex m;
15+
atomic_int ato_num{0};
16+
17+
void run() {
18+
for(int i=0; i<N; i++) {
19+
/*m.lock();
20+
num++;
21+
m.unlock();*/
22+
//ato_num++;
23+
num++;
24+
}
25+
}
26+
27+
void pause_thread(int n) {
28+
std::this_thread::sleep_for (std::chrono::seconds(n));
29+
std::cout << "pause of " << n << " seconds ended\n";
30+
}
31+
32+
void show(const char *str, const int id) {
33+
std::cout << "thread=" << id+1 << " : " << str << std::endl;
34+
}
35+
36+
void print() {
37+
cout << "hello test!" << endl;
38+
}
39+
40+
void fun1(int n) //初始化构造函数
41+
{
42+
cout << "Thread " << n << " executing\n";
43+
n += 10;
44+
this_thread::sleep_for(chrono::milliseconds(10));
45+
}
46+
47+
void fun2(int & n) //拷贝构造函数
48+
{
49+
cout << "Thread " << n << " executing\n";
50+
n += 20;
51+
this_thread::sleep_for(chrono::milliseconds(10));
52+
}
53+
54+
int main() {
55+
// join thread
56+
std::cout << "Spawning 3 threads...\n";
57+
std::thread t3(pause_thread, 3);
58+
std::thread t1(pause_thread, 1);
59+
std::thread t2(pause_thread, 2);
60+
std::cout << "Done spawning threads. Now waiting for them to join:\n";
61+
t1.join();
62+
t2.join();
63+
t3.join();
64+
std::cout << "All threads joined!\n";
65+
// thread detach
66+
std::cout << "Spawning and detaching 3 threads...\n";
67+
std::thread t4(pause_thread, 3);
68+
std::thread t5(pause_thread, 1);
69+
std::thread t6(pause_thread, 2);
70+
t4.detach();
71+
t5.detach();
72+
t6.detach();
73+
std::cout << "Done spawning threads.\n";
74+
75+
std::cout << "(the main thread will now pause for 5 seconds)\n";
76+
//give the detached threads time to finish (but not guaranteed!):
77+
pause_thread(5);
78+
79+
// thread joinable usage
80+
std::cout << "thread joinable usage \n";
81+
std::thread t;
82+
std::cout << "before starting joinable: " << t.joinable() << "\n";
83+
t = std::thread(pause_thread, 5);
84+
std::cout << "after starting, joinable: " << t.joinable() << "\n";
85+
t.join();
86+
std::cout << "after join, joinable: " << t.joinable() << "\n";
87+
88+
// swap thread
89+
std::thread t7(pause_thread, 10);
90+
std::thread::id t7_id = t7.get_id();
91+
92+
std::thread t8(pause_thread, 20);
93+
std::thread::id t8_id = t8.get_id();
94+
95+
std::cout << "t7 id: " << t7_id << "\n";
96+
std::cout << "t8 id: " << t8_id << "\n";
97+
98+
swap(t7, t8);
99+
100+
std::cout << "t7 id: " << t7.get_id() << "\n";
101+
std::cout << "t8 id: " << t8.get_id() << "\n";
102+
103+
t7.join();
104+
t8.join();
105+
106+
// thread different thread input
107+
int n = 0;
108+
thread t11; //t11不是一个thread
109+
thread t12(fun1, n + 1); //按照值传递
110+
t12.join();
111+
cout << "n=" << n << '\n';
112+
n = 10;
113+
thread t13(fun2, ref(n)); //引用
114+
thread t14(move(t13)); //t14执行t13,t13不是thread
115+
t14.join();
116+
cout << "n=" << n << '\n';
117+
118+
array<thread, 3> thread_array = {thread(print), thread(print), thread(print)};
119+
for(int i=0; i<3; i++) {
120+
cout << thread_array[i].joinable() << endl;
121+
thread_array[i].join();
122+
}
123+
124+
//thread cpu number
125+
auto cpu = thread::hardware_concurrency();
126+
cout << "thread cpu num " << cpu << endl;
127+
128+
// mutex usage
129+
clock_t start = clock();
130+
thread t15(run);
131+
//thread t16(run);
132+
t15.join();
133+
thread t16(run);
134+
t16.join();
135+
clock_t end = clock();
136+
cout << "num = " << num << " time = " << end-start << " ms" << endl;
137+
//cout << "ato num = " << ato_num << " time = " << end-start << " ms" << endl;
138+
139+
140+
141+
return 0;
142+
}
143+
144+

0 commit comments

Comments
 (0)