C++11 多线程< 一>、介绍

发布时间 2023-09-30 09:27:45作者: FeiYull

  

 1 #include <iostream>
 2 #include <thread>
 3 
 4 void fun1()
 5 {
 6     std::cout << "fuck"  << std::endl;
 7 }
 8 
 9 int main() // 主线程
10 {
11     std::thread t1(fun1); // t1 线程
12     //t1.join(); // 主线程和t1互不干扰,fun1执行
13 
14     t1.detach();//  主线程太快,fun1没执行,
15 
16     // 一个线程被detach之后,不能再join
17     if (t1.joinable())
18     {
19         t1.join();
20     }
21 
22     return 0;
23 }

 

 1 //
 2 // Created by sry on 2021/7/5.
 3 //
 4 #include <iostream>
 5 #include <thread>
 6 
 7 /*
 8  *
 9  * 线程管理
10  *
11  * */
12 
13 void fun1()
14 {
15     std::cout << "fuck"  << std::endl;
16 }
17 
18 int main() // 主线程
19 {
20     std::thread t1(fun1); // t1 线程
21 
22     // 主线程执行一下内容
23     for (int i = 0; i < 100; ++i)
24     {
25         std::cout << "str = " << i << std::endl;
26     }
27 
28 
29     t1.join(); // 主线程和t1互不干扰,fun1执行
30 
31     return 0;
32 }
类中添加一个参数,然后构造一个线程:
 1 //
 2 // Created by sry on 2021/7/5.
 3 //
 4 #include <iostream>
 5 #include <thread>
 6 
 7 /*
 8  *
 9  * 线程管理
10  *
11  * */
12 
13 void fun1()
14 {
15     std::cout << "fuck"  << std::endl;
16 }
17 
18 class  MyCl
19 {
20 public:
21     void operator()(std::string& msg)
22     {
23         for (int i = 0; i < 100; ++i) {
24             std::cout << "from t1   " <<  i << std::endl;
25           }
26     }
27 
28 };
29 
30 int main() // 主线程
31 {
32     MyCl mc;
33     std::string msg = "shiruiyu";
34     std::thread t1((MyCl()), msg); // t1 线程,类中添加一个参数,然后构造一个线程
35 
36     try
37     {
38         // 主线程执行一下内容
39         for (int i = 0; i < 100; ++i)
40         {
41             std::cout << "str = " << i << std::endl;
42         }
43     }
44     catch (...)
45     {
46         t1.join();
47         throw;
48     }
49     return 0;
50 }
View Code

  多线程中的值传递与引用传递,在main函数中,砸门要将参数msg和类MyCl一起构成线程t1,其中msg采用引用传递,必须做到两点:我都标红加粗了。

 1 //
 2 // Created by sry on 2021/7/5.
 3 //
 4 #include <iostream>
 5 #include <thread>
 6 
 7 /*
 8  *
 9  * 线程管理
10  *
11  * */
12 
13 void fun1()
14 {
15     std::cout << "fuck"  << std::endl;
16 }
17 
18 class  MyCl
19 {
20 public:
21     void operator()(std::string& msg)
22     {
23         std::cout << "from t1   "  << std::endl;
24         msg = "shiruiyu2";
25     }
26 
27 };
28 
29 int main() // 主线程
30 {
31     MyCl mc;
32     std::string msg = "shiruiyu1";
33     std::thread t1((MyCl()), std::ref(msg)); // t1 线程,类中添加一个参数,然后构造一个线程
34 
35     std::cout << "from main "  << std::endl;
36 
37     std::cout << "msg = " << msg  << std::endl; // shiruiyu1
38     t1.join();
39     std::cout << "msg = " << msg  << std::endl; // shiruiyu2
40     return 0;
41 }

c++多线程编程:join()函数与detch()函数的区别:

参考:https://blog.csdn.net/liunan199481/article/details/83055901

   线程只能被转移,不能被复制。

1 // error:线程只能被转移,不能被复制
2 //std::thread t2 = t1;
3 // 正确写法,转移线程
4 std::thread t2 = std::move(t1);
5 // 都转移了,这里join也要改为t2
6 t2.join(); // 理解为加入主线程

   输出主线程、其他线层ID

1 std::cout << "t1 thread = " << t1.get_id() << std::endl;
2 std::thread t2 = std::move(t1);
3 std::cout << "t2 thread = " << t2.get_id() << std::endl;
4 // 都转移了,这里join也要改为t2
5 t2.join(); // 理解为加入主线程
6 std::cout << "main thread = " << std::this_thread::get_id() << std::endl;

打印结果(1、t1转移到t2,ID不变;2、第2行代码要是放到第5行之后,打印显示线层不存在,表明执行t2.join之后,t2线程就销毁了):

t1 thread = 140545623889664
t2 thread = 140545623889664
from t1   
main thread = 140545641916224
// 获取CPU下线程数量
int n = std::thread::hardware_concurrency();