C++工程师面试宝典系列之多线程编程

来源:互联网 发布:北京ps软件培训班 编辑:程序博客网 时间:2024/04/29 08:01

多进程与多线程:


[cpp] view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. using namespace std;  
  4.   
  5. void function_1() {  
  6.     cout << "www.vcbh.bvn" << endl;  
  7.   
  8. }  
  9.   
  10. int main() {  
  11.     thread t1(function_1);  //t1子线程  
  12.     t1.detach();  //主线程执行得太快,子线程来不及执行  
  13.                   //detach之后就不能join  
  14.       
  15.     // ...  
  16.   
  17.     if (t1.joinable())  
  18.     {  
  19.         t1.join();  
  20.     }  
  21.           
  22.     return 0;  
  23. }  

结论:一个线程t1只能被join()或者detach()一次。

=======================================================================================

2.线程管理

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. using namespace std;  
  5.   
  6. void function_1() {  
  7.     cout << "www.vcbh.bvn" << endl;  
  8.   
  9. }  
  10.   
  11. class Fctor{  
  12. public:  
  13.     void operator()(string& msg){   //通过应用可以减少很多的赋值操作  
  14.         for (int i = 0; i > -100; i--)  
  15.             cout << "from t1:" << msg << endl;  
  16.     }  
  17. };  
  18.   
  19.   
  20. int main() {  
  21. /*  Fctor fct;  //由一个类构造一个线程 
  22.     thread t1(fct);  //t1子线程开始运行 
  23. */  
  24.     string s = "i l nhfjf";  
  25.     thread t1((Fctor()),s);  
  26.     try{  
  27.         for (int i = 0; i < 100; i++)  
  28.         {  
  29.             cout << "from main:" << i << endl;  
  30.         }  
  31.     }  
  32.     catch (...)  
  33.     {  
  34.         t1.join();  
  35.         throw;  
  36.     }  
  37.       
  38.     t1.join();  
  39.       
  40.           
  41.     return 0;  
  42. }  

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. using namespace std;  
  5.   
  6. void function_1() {  
  7.     cout << "www.vcbh.bvn" << endl;  
  8.   
  9. }  
  10.   
  11. class Fctor{  
  12. public:  
  13.     void operator()(string& msg){   //通过应用可以减少很多的赋值操作  
  14.             cout << "from t1:" << msg << endl;  
  15.             msg = "hehuanlin";  
  16.     }  
  17. };  
  18.   
  19.   
  20. int main() {  
  21. /*  Fctor fct;  //由一个类构造一个线程 
  22.     thread t1(fct);  //t1子线程开始运行 
  23. */  
  24.     string s = "i l nhfjf";  
  25.     thread t1((Fctor()),ref(s));  
  26.       
  27.       
  28.     t1.join();  
  29.     //cout<<"from main:"<<s<<endl;  
  30.           
  31.     return 0;  
  32. }  

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <thread>  
  3. #include <string>  
  4. using namespace std;  
  5.   
  6. void function_1() {  
  7.     cout << "www.vcbh.bvn" << endl;  
  8.   
  9. }  
  10.   
  11. class Fctor{  
  12. public:  
  13.     void operator()(string& msg){   //通过应用可以减少很多的赋值操作  
  14.             cout << "from t1:" << msg << endl;  
  15.             msg = "hehuanlin";  
  16.     }  
  17. };  
  18.   
  19.   
  20. int main() {  
  21. /*  Fctor fct;  //由一个类构造一个线程 
  22.     thread t1(fct);  //t1子线程开始运行 
  23. */  
  24.     string s = "i l nhfjf";  
  25.     cout << this_thread::get_id() << endl;  //主线程的id  
  26.   
  27.     thread t1((Fctor()),move(s));  
  28.       
  29.     thread t2 = move(t1);  //线程只能被移动,不能被赋值  
  30.     cout << t2.get_id() << endl;  //t2线程的id  
  31.   
  32.     t2.join();  
  33.     //cout<<"from main:"<<s<<endl;  
  34.     thread::hardware_concurrency();  //告诉最多可以使用多少个线程      
  35.          return 0;  
  36. }  


=======================================================================================

3.数据竞争与互斥对象


=======================================================================================

4.死锁


=======================================================================================

5.Unique  Lock 和Lazy  Init


=======================================================================================

6.条件变量


=======================================================================================

7.Future , Promise 和asyn


=======================================================================================

8.使用可调用对象


=======================================================================================

9.packaged_task


=======================================================================================

10.回顾和时间约束

0 0
原创粉丝点击