#include


 

在這個庫最重要的一個類就是boost::thread,它是在boost/thread.hpp里定義的,用來創建一個新線程。它已經被納入C++標准庫中。

 

小結:新一代C++標准將線程庫引入后,將簡化多線程開發。

 

 1 #include <iostream>
 2 #include <boost/thread.hpp>
 3 
 4 void wait(int sec)
 5 {
 6     boost::this_thread::sleep(boost::posix_time::seconds(sec));//boost的sleep可以跨平台,但是windows的sleep不可以跨平台
 7 }
 8 
 9 void threadA()
10 {
11     for (int i = 0; i < 10; i++)
12     {
13         wait(1);
14         std::cout << i << std::endl;
15     }
16 }
17 
18 void threadB()
19 {
20     try
21     {
22         for (int i = 0; i < 10; i++)
23         {
24             wait(1);
25             std::cout << i << std::endl;
26         }
27     }
28     catch (boost::thread_interrupted &)
29     {
30 
31     }
32 }
33 
34 void main()
35 {
36     boost::thread t(threadA);
37     wait(3);
38     t.interrupt();//結束進程
39     t.join();
40 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM