C++使用thread类多线程编程

释放双眼,带上耳机,听听看~!

C++11中引入了一个用于多线程操作的thread类,简单多线程示例:


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
1#include <iostream>
2#include <thread>
3#include <Windows.h>
4
5using namespace std;
6
7void thread01()
8{
9   for (int i = 0; i < 5; i++)
10  {
11      cout << "Thread 01 is working !" << endl;
12      Sleep(100);
13  }
14}
15void thread02()
16{
17  for (int i = 0; i < 5; i++)
18  {
19      cout << "Thread 02 is working !" << endl;
20      Sleep(200);
21  }
22}
23
24int main()
25{
26  thread task01(thread01);
27  thread task02(thread02);
28  task01.join();
29  task02.join();
30
31  for (int i = 0; i < 5; i++)
32  {
33      cout << "Main thread is working !" << endl;
34      Sleep(200);
35  }
36  system("pause");
37}
38

C++使用thread类多线程编程

两个子线程并行执行,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
1#include <iostream>
2#include <thread>
3#include <Windows.h>
4
5using namespace std;
6
7void thread01()
8{
9   for (int i = 0; i < 5; i++)
10  {
11      cout << "Thread 01 is working !" << endl;
12      Sleep(100);
13  }
14}
15void thread02()
16{
17  for (int i = 0; i < 5; i++)
18  {
19      cout << "Thread 02 is working !" << endl;
20      Sleep(200);
21  }
22}
23
24int main()
25{
26  thread task01(thread01);
27  thread task02(thread02);
28  task01.detach();
29  task02.detach();
30
31  for (int i = 0; i < 5; i++)
32  {
33      cout << "Main thread is working !" << endl;
34      Sleep(200);
35  }
36  system("pause");
37}
38

C++使用thread类多线程编程

使用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
1#include <iostream>
2#include <thread>
3#include <Windows.h>
4
5using namespace std;
6
7//定义带参数子线程
8void thread01(int num)
9{
10  for (int i = 0; i < num; i++)
11  {
12      cout << "Thread 01 is working !" << endl;
13      Sleep(100);
14  }
15}
16void thread02(int num)
17{
18  for (int i = 0; i < num; i++)
19  {
20      cout << "Thread 02 is working !" << endl;
21      Sleep(200);
22  }
23}
24
25int main()
26{
27  thread task01(thread01, 5);  //带参数子线程
28  thread task02(thread02, 5);
29  task01.detach();
30  task02.detach();
31
32  for (int i = 0; i < 5; i++)
33  {
34      cout << "Main thread is working !" << endl;
35      Sleep(200);
36  }
37  system("pause");
38}
39

C++使用thread类多线程编程

 

 

多线程数据竞争

多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:


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
1#include <iostream>
2#include <thread>
3#include <Windows.h>
4
5using namespace std;
6
7int totalNum = 100;
8
9void thread01()
10{
11  while (totalNum > 0)
12  {
13      cout << totalNum << endl;
14      totalNum--;
15      Sleep(100);
16  }
17}
18void thread02()
19{
20  while (totalNum > 0)
21  {
22      cout << totalNum << endl;
23      totalNum--;
24      Sleep(100);
25  }
26}
27
28int main()
29{
30  thread task01(thread01);
31  thread task02(thread02);
32  task01.detach();
33  task02.detach();
34  system("pause");
35}
36

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
1100100
2
3请按任意键继续. . . 98
498
59696
6
794
894
99292
10
119090
12
1388
1488
158686
16
178484
18
1982
2082
2180
2280
2378
2478
257676
26
2774
2874
2972
3072
317070
32
336868
34
3566
3666
376464
38
3962
4062
4160
4260
435858
44
4556
4656
475454
48
495252
50
5150
5250
5348
5448
554646
56
5744
5844
594242
60
614040
62
6338
6438
6536
6636
673434
68
693232
70
7130
7230
732828
74
752626
76
772424
78
792222
80
8120
8220
8318
8418
851616
86
8714
8814
891212
90
911010
92
938
947
956
965
974
983
992
1001
101

有两个问题,一是有很多变量被重复输出了,而有的变量没有被输出;二是正常情况下每个线程输出的数据后应该紧跟一个换行符,但这里大部分却是另一个线程的输出。

这是由于第一个线程对变量操作的过程中,第二个线程也对同一个变量进行各操作,导致第一个线程处理完后的输出有可能是线程二操作的结果。针对这种数据竞争的情况,可以使用线程互斥对象mutex保持数据同步

mutex类的使用需要包含头文件mutex


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
1#include <iostream>
2#include <thread>
3#include <Windows.h>
4#include <mutex>
5
6using namespace std;
7
8mutex mu;  //线程互斥对象
9
10int totalNum = 10;
11
12void thread01()
13{
14  while (totalNum > 0)
15  {
16      mu.lock(); //同步数据锁
17      cout << totalNum << endl;
18      totalNum--;
19      Sleep(100);
20      mu.unlock();  //解除锁定
21  }
22}
23void thread02()
24{
25  while (totalNum > 0)
26  {
27      mu.lock();
28      cout << totalNum << endl;
29      totalNum--;
30      Sleep(100);
31      mu.unlock();
32  }
33}
34
35int main()
36{
37  thread task01(thread01);
38  thread task02(thread02);
39  task01.detach();
40  task02.detach();
41  system("pause");
42}
43

 

多线程中加入mutex互斥对象之后输出正常:

C++使用thread类多线程编程

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列C/C++ Addons(1) API文档

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索