首页 > 编程语言 > 详细

C++多线程

时间:2019-09-02 22:27:07      阅读:72      评论:0      收藏:0      [点我收藏+]

目录

1、简单多线程实例(使用join())

2、简单多线程实例(使用detach())

3、带参子线程

4、多线程竞争数据,使用mutex阻止多线程之间数据的竞争

1、简单多线程实例(使用join())

使用thread创建子线程,并立马使用jion()阻塞住,直到子线程执行完毕;(两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程)代码如下:

技术分享图片
 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01()
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     Sleep(100);
11     cout << "Thread 01 is sleeping over !" << endl;
12 }
13 void thread02()
14 {
15     cout << "Thread 02 is working !" << endl;
16     Sleep(100);
17     cout << "Thread 02 is sleeping over !" << endl;
18 }
19 
20 int main()
21 {
22     thread task01(thread01);  //创建子线程并执行该子线程
23     thread task02(thread02);
24     task01.join();  //等待线程task01执行完毕之后,再去执行下一句
25     task02.join();  //等待线程task02执行完毕之后,再去执行下一句
26     for (int i = 0; i < 2; i++)
27     {
28         cout << "Main thread is working !" << endl;
29         Sleep(200);
30     }
31 
32     system("pause");
33 }
简单子线程实例

执行结果:

技术分享图片 执行第二遍:技术分享图片

 

 2、简单多线程实例---使用detach()

detach将子线程从主流程中分离,独立运行,不会阻塞主线程:

技术分享图片
 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01()
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     Sleep(100);
11     cout << "Thread 01 is sleeping over !" << endl;
12 }
13 void thread02()
14 {
15     cout << "Thread 02 is working !" << endl;
16     Sleep(100);
17     cout << "Thread 02 is sleeping over !" << endl;
18 }
19 
20 int main()
21 {
22     thread task01(thread01);  //创建子线程并执行该子线程
23     thread task02(thread02);
24     task01.detach();  //等待线程task01执行完毕之后,再去执行下一句
25     task02.detach();  //等待线程task02执行完毕之后,再去执行下一句
26     for (int i = 0; i < 2; i++)
27     {
28         cout << "Main thread is working !" << endl;
29         Sleep(200);
30     }
31 
32     system("pause");
33 }
简单多线程实例---使用detach()

技术分享图片

 

3、带参子线程

技术分享图片
 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 
 5 using namespace std;
 6 
 7 void thread01(int num)
 8 {
 9     cout << "Thread 01 is working !" << endl;
10     cout << "Thread01‘s sleeping time is " << num << endl;
11     Sleep(100);
12     cout << "Thread 01 is sleeping over !" << endl;
13 }
14 void thread02(int num)
15 {
16     cout << "Thread 02 is working !" << endl;
17     cout << "Thread02‘s sleeping time is " << num << endl;
18     Sleep(300);
19     cout << "Thread 02 is sleeping over !" << endl;
20 }
21 
22 int main()
23 {
24     thread task01(thread01,100);  //创建子线程并执行该子线程
25     thread task02(thread02,300);
26     task01.join();  //等待线程task01执行完毕之后,再去执行下一句
27     task02.join();  //等待线程task02执行完毕之后,再去执行下一句
28     for (int i = 0; i < 2; i++)
29     {
30         cout << "Main thread is working !" << endl;
31         Sleep(200);
32     }
33 
34     system("pause");
35 }
带参子线程

技术分享图片

 

 4、多线程竞争数据,使用mutex阻止多线程之间数据的竞争

因为在线程1和线程2只均改变了全局变量totalNum
为了在使多线程之间相互影响,需要声明一个互斥对象保持数据同步

技术分享图片
 1 #include <iostream>
 2 #include <thread>
 3 #include <Windows.h>
 4 #include <mutex>
 5 
 6 using namespace std;
 7 int totalNum = 100;
 8 
 9 /*
10 因为在线程1和线程2只均改变了全局变量totalNum
11 为了在使多线程之间相互影响,需要声明一个互斥对象保持数据同步
12 */
13 std::mutex mu;  //声明线程互斥对象
14 
15 void thread01(int num)
16 {
17     mu.lock();  //同步数据锁
18     cout << totalNum << endl;
19     totalNum--;
20     Sleep(100);
21     mu.unlock();  //解除锁定
22 }
23 void thread02(int num)
24 {
25     mu.lock();  //同步数据锁
26     cout << totalNum << endl;
27     totalNum--;
28     Sleep(300);
29     mu.unlock();  //解除锁定
30 }
31 
32 int main()
33 {
34     thread task01(thread01,100);  //创建子线程并执行该子线程
35     thread task02(thread02,300);
36     task01.join();  //等待线程task01执行完毕之后,再去执行下一句
37     task02.join();  //等待线程task02执行完毕之后,再去执行下一句
38     for (int i = 0; i < 2; i++)
39     {
40         cout << "Main thread is working !" << endl;
41         Sleep(200);
42     }
43 
44     system("pause");
45 }
metux阻止多线程数据相互影响

技术分享图片

 

C++多线程

原文:https://www.cnblogs.com/YiYA-blog/p/11449140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!