c++中關於std::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的調用位置就比較關鍵。比如,以下的調用位置都是錯誤的。
例子一:
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的資源沒有被回收,造成了資源泄露。
例子二:
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啦。
例子三:
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
不會被觸發捕捉異常,同時造成程序直接從函數調用棧回溯返回,也不會調用到join
,也會造成線程資源沒被回收,資源泄露。
所以在這里有一個方法是使用創建局部對象,利用函數調用棧的特性,確保對象被銷毀時觸發析構函數的方法來確保在主線程結束前調用join()
,等待回收創建的線程的資源。
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
操作對於同一個線程只能調用一次,不然會出現錯誤的。這樣,就可以確保線程一定會在主函數結束前被等待回收了。