c++11中關於std::thread的join的思考

c++中關於std::thread的join的思考c++

std::thread是c++11新引入的線程標準庫,經過其能夠方便的編寫與平臺無關的多線程程序,雖然對比針對平臺來定製化多線程庫會使性能達到最大,可是會喪失了可移植性,這樣對比其餘的高級語言,可謂是一個不足。終於在c++11認可多線程的標準,可謂可喜可賀!!!多線程

在使用std::thread的時候,對建立的線程有兩種操做:等待/分離,也就是join/detach操做。join()操做是在std::thread t(func)後「某個」合適的地方調用,其做用是回收對應建立的線程的資源,避免形成資源的泄露。detach()操做是在std::thread t(func)後立刻調用,用於把被建立的線程與作建立動做的線程分離,分離的線程變爲後臺線程,其後,建立的線程的「死活」就與其作建立動做的線程無關,它的資源會被init進程回收。函數

在這裏主要對join作深刻的理解。性能

因爲join是等待被建立線程的結束,並回收它的資源。所以,join的調用位置就比較關鍵。好比,如下的調用位置都是錯誤的。線程

例子一:c++11

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的資源沒有被回收,形成了資源泄露。code

例子二:對象

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啦。
例子三:ci

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操做對於同一個線程只能調用一次,否則會出現錯誤的。這樣,就能夠確保線程必定會在主函數結束前被等待回收了。

相關文章
相關標籤/搜索