手撕代码之线程: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 }

运行结果:

猜你喜欢

转载自www.cnblogs.com/yichengming/p/11506395.html