Thread中,join()方法
std::thread是c++11新引入的線程標準庫,通過其可以方便的編寫與平臺無關的多線程程序,雖然對比針對平臺來定制化多線程庫會使性能達到最大,但是會喪失了可移植性,這樣對比其他的高級語言,可謂是一個不足。終于在c++11承認多線程的標準,可謂可喜可賀!!!
在使用std::thread的時候,對創建的線程有兩種操作:等待/分離,也就是join/detach操作。join()操作是在std::thread t(func)后“某個”合適的地方調用,其作用是回收對應創建的線程的資源,避免造成資源的泄露。detach()操作是在std::thread t(func)后馬上調用,用于把被創建的線程與做創建動作的線程分離,分離的線程變為后臺線程,其后,創建的線程的“死活”就與其做創建動作的線程無關,它的資源會被init進程回收。
在這里主要對join做深入的理解。
由于join是等待被創建線程的結束,并回收它的資源。因此,join的調用位置就比較關鍵。比如,以下的調用位置都是錯誤的。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | void test() { } ? bool do_other_things() { } ? int main() { ??std::thread t(test); ??int ret = do_other_things(); ??if(ret == ERROR) { ????return -1; ??} ? ??t.join(); ??return 0; } | 
很明顯,如果do_other_things()函數調用返ERROR, 那么就會直接退出main函數,此時join就不會被調用,所以線程t的資源沒有被回收,造成了資源泄露。
例子二:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | void test() { } ? bool do_other_things() { } ? int main() { ??std::thread t(test); ? ??try { ????do_other_things(); ??} ??catch(...) { ????throw; ??} ??t.join(); ??return 0; } | 
這個例子和例子一差不多,如果調用do_other_things()函數拋出異常,那么就會直接終止程序,join也不會被調用,造成了資源沒被回收。
那么直接在異常捕捉catch代碼塊里調用join就ok啦。
例子三:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | void test() { } ? bool do_other_things() { } ? int main() { ??std::thread t(test); ? ??try { ????do_other_things(); ??} ??catch(...) { ????t.join(); ????throw; ??} ??t.join(); ??return 0; } | 
是不是很多人這樣操作?這樣做不是萬無一失的,?try/catch塊只能夠捕捉輕量級的異常錯誤,在這里如果在調用do_other_things()時發生嚴重的異常錯誤,那么catch不會被觸發捕捉異常,同時造成程序直接從函數調用?;厮莘祷?#xff0c;也不會調用到join,也會造成線程資源沒被回收,資源泄露。
所以在這里有一個方法是使用創建局部對象,利用函數調用棧的特性,確保對象被銷毀時觸發析構函數的方法來確保在主線程結束前調用join(),等待回收創建的線程的資源。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class mythread { private: ??std::thread &m_t; ? public: ??explicit mythread(std::thread &t):m_t(t){} ??~mythread() { ????if(t.joinable()) { ??????t.join() ????} ??} ? ??mythread(mythread const&) = delete; ??mythread& operate=(mythread const&) = delete; } ? void test() { } ? bool do_other_things() { } ? int main() { ??std::thread t(test); ??mythread q(t); ? ??if(do_other_things()) { ????return -1; ??} ? ??return 0; } | 
在上面的例子中,無論在調用do_other_things()是發生錯誤,造成return main函數,還是產生異常,由于函數調用棧的關系,總會回溯的調用局部對象q的析構函數,同時在q的析構函數里面先判斷j.joinable()是因為join操作對于同一個線程只能調用一次,不然會出現錯誤的。這樣,就可以確保線程一定會在主函數結束前被等待回收了。
以上所述是小編給大家介紹的c++11中關于std::thread的join詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:
- C++11并發編程:多線程std::thread
- C++11 并發指南之std::thread 詳解
總結
以上是生活随笔為你收集整理的Thread中,join()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Shell脚本读取Redis键值对
- 下一篇: C++11中Thread类简单使用的例子
