手撕代码之线程:thread类简单使用

转载于:https://blog.csdn.net/qq_22494029/article/details/79273127

简单多线程例子:

detch()启动线程:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1();
 5 void testThread2();
 6 int main()
 7 {
 8     thread t1(testThread1);
 9     t1.detach();
10     thread t2(testThread2);
11     t2.detach();
12     cout<<"主线程:你好帅!!!"<<endl;
13     std::this_thread::sleep_for(std::chrono::milliseconds(10000));
14     return 0;
15 }
16 void testThread1()//线程1
17 {
18     for (int i = 0; i <10 ; ++i) {
19         cout<<"testThread1:"<<i<<endl;
20         //暂停100毫秒
21         std::this_thread::sleep_for(std::chrono::milliseconds(100));
22     }
23 }
24 void testThread2()//线程2
25 {
26     for (int i = 100; i < 110; ++i) {
27         cout<<"testThread2:"<<i<<endl;
28         this_thread::sleep_for(chrono::milliseconds(100));
29     }
30 }

说明:detch()方法的意思就是开启子线程,并且主线程不等待子线程运行完毕,而是和子线程并行运行。

运行结果:

 

 

join()方法启动线程:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1();
 5 void testThread2();
 6 int main()
 7 {
 8     thread t1(testThread1);
 9     t1.join();
10     thread t2(testThread2);
11     t2.join();
12     cout<<"主线程:你好帅!!!"<<endl;
13     return 0;
14 }
15 void testThread1()//线程1
16 {
17     for (int i = 0; i <10 ; ++i) {
18         cout<<"testThread1:"<<i<<endl;
19         //暂停100毫秒
20         std::this_thread::sleep_for(std::chrono::milliseconds(100));
21     }
22 }
23 void testThread2()//线程2
24 {
25     for (int i = 100; i < 110; ++i) {
26         cout<<"testThread2:"<<i<<endl;
27         this_thread::sleep_for(chrono::milliseconds(100));
28     }
29 }

说明:join()方法的意思就是开启子线程,线程会按照开启的先后顺序同步运行。

运行结果:

 

 

子线程函数带有参数的多线程:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1(int count);
 5 void testThread2(int start,int count);
 6 int main()
 7 {
 8     thread t1(testThread1,10);
 9     t1.join();
10     thread t2(testThread2,40,50);
11     t2.join();
12     cout<<"主线程:你好帅!!!"<<endl;
13     return 0;
14 }
15 void testThread1(int count)
16 {
17     for (int i = 0; i <count; ++i) {
18         cout<<"testThread1:"<<i<<endl;
19         this_thread::sleep_for(chrono::milliseconds(100));
20     }
21 }
22 void testThread2(int start,int count)
23 {
24     for (int i = start; i <count ; ++i) {
25         cout<<"testThread2:"<<i<<endl;
26         this_thread::sleep_for(chrono::milliseconds(100));
27     }
28 }

运行结果:

 

 多线程安全访问共享数据例子(卖票)

 1 //多线程安全访问共享数据例子(卖票)
 2 #include <iostream>
 3 #include <thread>
 4 #include <mutex>
 5 using namespace std;
 6 class ThreadTest
 7 {
 8 private:
 9     //票的剩余数目
10     int sum;
11     mutex Mutex;
12 public:
13     //构造函数,构造的时候开启线程
14     ThreadTest()
15     {
16         sum=20;
17         thread t1(&ThreadTest::Thread1,this);
18         t1.detach();
19         thread t2(&ThreadTest::Thread2,this);
20         t2.detach();
21     }
22     //析构函数
23     ~ThreadTest(){}
24 
25     //卖票线程1
26     void Thread1()
27     {
28         while(true)
29         {
30 
31             this_thread::sleep_for(chrono::milliseconds(100));
32             Mutex.lock();//加锁
33             --sum;
34             if(sum<0)
35             {
36                 cout<<"Thread1--票卖完了"<<sum<<endl;
37                 break;
38             }
39             cout<<"Thread1--剩余票数:"<<sum<<endl;
40             Mutex.unlock();
41         }
42         Mutex.unlock();
43     }
44     //卖票线程2
45     void Thread2()
46     {
47         while(true)
48         {
49             this_thread::sleep_for(chrono::milliseconds(100));
50             Mutex.lock();//加锁
51             sum--;
52             if(sum<0)
53             {
54                 cout<<"Thread2--票卖完了"<<sum<<endl;
55                 break;
56             }
57             cout<<"Thread2--剩余票数:"<<sum<<endl;
58             Mutex.unlock();
59         }
60         Mutex.unlock();
61     }
62 };
63 int main()
64 {
65     //多线程卖票类
66     ThreadTest SaleThread;
67     this_thread::sleep_for(chrono::milliseconds(10000));
68     return 0;
69 }

运行结果:

 

转载于:https://www.cnblogs.com/yichengming/p/11506395.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值