c++11中關(guān)于std::thread的join的詳解
std::thread
是c++11新引入的線程標(biāo)準(zhǔn)庫,通過其可以方便的編寫與平臺無關(guān)的多線程程序,雖然對比針對平臺來定制化多線程庫會使性能達(dá)到最大,但是會喪失了可移植性,這樣對比其他的高級語言,可謂是一個不足。終于在c++11承認(rèn)多線程的標(biāo)準(zhǔn),可謂可喜可賀?。?!
在使用std::thread
的時候,對創(chuàng)建的線程有兩種操作:等待/分離,也就是join/detach
操作。join()
操作是在std::thread t(func)
后“某個”合適的地方調(diào)用,其作用是回收對應(yīng)創(chuàng)建的線程的資源,避免造成資源的泄露。detach()
操作是在std::thread t(func)
后馬上調(diào)用,用于把被創(chuàng)建的線程與做創(chuàng)建動作的線程分離,分離的線程變?yōu)楹笈_線程,其后,創(chuàng)建的線程的“死活”就與其做創(chuàng)建動作的線程無關(guān),它的資源會被init
進(jìn)程回收。
在這里主要對join
做深入的理解。
由于join
是等待被創(chuàng)建線程的結(jié)束,并回收它的資源。因此,join的調(diào)用位置就比較關(guān)鍵。比如,以下的調(diào)用位置都是錯誤的。
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()
函數(shù)調(diào)用返ERROR, 那么就會直接退出main函數(shù)
,此時join就不會被調(diào)用,所以線程t的資源沒有被回收,造成了資源泄露。
例子二:
void test() { } bool do_other_things() { } int main() { std::thread t(test); try { do_other_things(); } catch(...) { throw; } t.join(); return 0; }
這個例子和例子一差不多,如果調(diào)用do_other_things()
函數(shù)拋出異常,那么就會直接終止程序,join
也不會被調(diào)用,造成了資源沒被回收。
那么直接在異常捕捉catch
代碼塊里調(diào)用join
就ok啦。
例子三:
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
塊只能夠捕捉輕量級的異常錯誤,在這里如果在調(diào)用do_other_things()
時發(fā)生嚴(yán)重的異常錯誤,那么catch
不會被觸發(fā)捕捉異常,同時造成程序直接從函數(shù)調(diào)用棧回溯返回,也不會調(diào)用到join
,也會造成線程資源沒被回收,資源泄露。
所以在這里有一個方法是使用創(chuàng)建局部對象,利用函數(shù)調(diào)用棧的特性,確保對象被銷毀時觸發(fā)析構(gòu)函數(shù)的方法來確保在主線程結(jié)束前調(diào)用join()
,等待回收創(chuàng)建的線程的資源。
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; }
在上面的例子中,無論在調(diào)用do_other_things()
是發(fā)生錯誤,造成return main
函數(shù),還是產(chǎn)生異常,由于函數(shù)調(diào)用棧的關(guān)系,總會回溯的調(diào)用局部對象q的析構(gòu)函數(shù),同時在q的析構(gòu)函數(shù)里面先判斷j.joinable()
是因為join
操作對于同一個線程只能調(diào)用一次,不然會出現(xiàn)錯誤的。這樣,就可以確保線程一定會在主函數(shù)結(jié)束前被等待回收了。
以上所述是小編給大家介紹的c++11中關(guān)于std::thread的join詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!