C++多线程编程第二讲:线程启动、结束,创建线程多法,join,detach

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*线程启动、结束,创建线程多法,join,detach*/
//(1)线程运行的开始和结束
//主线程从main为入口。那我们自己创建的线程也需要从一个函数为入口。
// 当没有detach的时候,整个程序执行结束的标志是主线程执行结束。
  //(1.1)thread
#include<iostream>
#include<thread> //多线成引用的头文件
 
using namespace std;
 
void my_print()
{
cout << "my thread is running..." << endl;
//...
cout << "my thread is end!" << endl;
}
 
int main()
{
thread my_thread(my_print); //创建线程,并执行
my_thread.join(); //主线程需要在这里等待子线程在这里执行结束
 
cout << "main thread is end!" << endl;
return 0;
}
 
  //(1.2)join() :等待子线程运行完毕
  //(1.3)detach() :不等待子线程执行结束,子线程交给C++运行时库,在后台运行。执行detach之后,与主线程关联的thread
  // 对象,就会失去关联。
 
  //(1.4)joinable()  判断是否可以成功使用join或者detach的。
 
#include<iostream>
#include<thread> //多线成引用的头文件
 
using namespace std;
 
void my_print()
{
cout << "my thread is running..." << endl;
//...
cout << "my thread is end!" << endl;
}
 
int main()
{
thread my_thread(my_print);
if (my_thread.joinable())
{
my_thread.join();
}
 
cout << "main thread is end!" << endl;
return 0;
}
 
//(2)其他创建线程的方法
 
  //(2.1)用法、以及一个问题范例
 
#include<iostream>
#include<thread> //多线成引用的头文件
 
using namespace std;
 
class TA
{
public:
void operator()() //让类成为一个可调用对象
{
cout << "TA的operator方法开始执行..." << endl;
}
};
 
int main()
{
TA ta;
thread mytobj3(ta);
if (mytobj3.joinable())
{
mytobj3.join();
}
 
cout << "main thread is end!" << endl;
return 0;
}
 
  // 注意的一个问题:
 
#include<iostream>
#include<thread> //多线成引用的头文件
 
using namespace std;
 
class TA
{
public:
int& m_i;    //这个地方使用引用会出现问题
TA(int& i) :m_i(i) {}
void operator()() //让类成为一个可调用对象
{
cout << "m_i = " << m_i << endl;
}
};
 
int main()
{
int myi = 6;         //当主线程中的这个值的内存被释放之后,创建的线程的中的引用也会发生异常
TA ta(myi);        
thread mytobj3(ta);   //ta调用了拷贝构造函数。
mytobj3.detach();
 
cout << "main thread is end!" << endl;
return 0;
}
 
  
 
 
#include<iostream>
#include<thread> //多线成引用的头文件
 
using namespace std;
 
class TA
{
public:
int& m_i; //这个引用会引起未知的错误
TA(int& i) :m_i(i)
{
cout << "TA的构造函数执行..." << endl;
}
 
TA(const TA& ta) :m_i(ta.m_i)
{
cout << "TA 的拷贝构造函数执行..." << endl;
}
 
~TA()
{
cout << "TA 的析构函数执行..." << endl;
}
 
void operator()() //让类成为一个可调用对象
{
cout << "m_i = " << m_i << endl;
}
};
 
int main()
{
int myi = 6;
TA ta(myi);
thread mytobj3(ta);
mytobj3.detach();
 
cout << "main thread is end!" << endl;
return 0;
}
 
//(2.2)用lambda表达式
 
#include<iostream>
#include<thread> //多线程引用的头文件
 
using namespace std;
 
int main()
{
auto mylamthread = [] {
cout << "mylamthread thread start..." << endl;
};
 
thread mythread(mylamthread);
if (mythread.joinable())
{
mythread.join();
}
 
cout << "main thread is end!" << endl;
return 0;
}
 
  
 
  
 
  

  

posted on   xcxfury001  阅读(143)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示