2013-03-01 1 views
21

*thisによって識別されるスレッドまで、現在のスレッドが実行を終了 std :: thread.join()は何をしますか? <a href="http://en.cppreference.com/w/cpp/thread/thread/join" rel="noreferrer">definition from C++ reference</a>によって

ブロック

.join()を使用して、そのスレッドがいくつかの関数を呼び出す mutex.lock()する必要はありませんとき

は、これが意味するのでしょうか?私は相互排除とスレッディングには新しいので、混乱しています。

注:私は本の中で C++ Concurrency in Actionを見つけましたが、私はその本を読んでいます。私のようなマルチスレッドの初心者のために書かれています。

ありがとうございました。

+3

デビッドに編集を依頼していただきありがとうございます。 Martin:私はグーグルにしようとしましたが、同じアイデアを持ち、参加が私の視点から行っていることを実際に示していない4-5個のサンプルを見ました。私はここでpplが知識があり、コードで最高の回答を提供すると思いましたサンプル。 – Guagua

答えて

17

あなたはまだmutexと条件が必要です。スレッドに参加すると、実行中のあるスレッドが別のスレッドの実行を待機します。共有リソースを保護するには、引き続きmutexが必要です。この例のmain()は、すべてのスレッドが終了してから終了するまで待つことができます。

#include <iostream> 
#include <thread> 
#include <chrono> 
#include <mutex> 

using namespace std; 



int global_counter = 0; 
std::mutex counter_mutex; 

void five_thread_fn(){ 
    for(int i = 0; i<5; i++){ 
     counter_mutex.lock(); 
     global_counter++; 
     counter_mutex.unlock(); 
     std::cout << "Updated from five_thread" << endl; 
     std::this_thread::sleep_for(std::chrono::seconds(5)); 
    } 
    //When this thread finishes we wait for it to join 
} 

void ten_thread_fn(){ 
    for(int i = 0; i<10; i++){ 
     counter_mutex.lock(); 
     global_counter++; 
     counter_mutex.unlock(); 
     std::cout << "Updated from ten_thread" << endl; 
     std::this_thread::sleep_for(std::chrono::seconds(1)); 
    } 
    //When this thread finishes we wait for it to join 
} 
int main(int argc, char *argv[]) { 
    std::cout << "starting thread ten..." << std::endl; 
    std::thread ten_thread(ten_thread_fn); 

    std::cout << "Running ten thread" << endl; 
    std::thread five_thread(five_thread_fn); 


    ten_thread.join(); 
    std::cout << "Ten Thread is done." << std::endl; 
    five_thread.join(); 
    std::cout << "Five Thread is done." << std::endl; 
} 

出力は次のようになりますことをご注意:

starting thread ten... 
Running ten thread 
Updated frUopmd atteend_ tfhrroema df 
ive_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from five_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from five_thread 
Ten Thread is done. 
Updated from five_thread 
Updated from five_thread 
Five Thread is done. 

のstd :: coutのがあるので、それの共有リソースへのアクセスおよび使用であるにもmutexがあまりにも保護する必要があります。

+1

Joelさん、ありがとうございます。ここでten_thread.join()はten_threadが完了したことを伝え、次のステップに進むことができますか? – Guagua

+1

main()を実行している実行スレッドは、ten_threadが戻るまでten_thread.join()の呼び出しでブロックされます。 ten_threadの実行が終了すると、メインの実行が続行されます。お役に立てれば。 – Joel

6

join()は、別のものが終了するまで現在のスレッドを停止します。 mutex所有者がロックを解除するまで、mutexは現在のスレッドを停止し、ロックされていなければすぐにロックします。だからこれらの人はかなり異なっている

+6

私は "停止"の代わりに "中断"を好む。 – Sebastian

0

のstd :: thread.joinは3つの、私はオフの手を考えることができる機能や他のいくつかを持っています

a)は、そのパフォーマンスを打撃し、リークのprobabiltyを増やし、スレッドの破棄/継続的な作成/終了を奨励しますスレッド暴走、メモリ暴走、一般的なアプリケーションの制御喪失などがあります。

b)不要な待機を強制することでGUIイベントハンドラを詰め込み、顧客が嫌う「砂時計アプリ」が応答しなくなります。

c)未解決の、中断されないスレッドの終了を待っているため、アプリケーションがシャットダウンできません。

d)その他の悪いもの。

私はあなたがマルチスレッドの初心者であることを理解しています。また、私は今夜多くのAdnams Broadsideを持っていたと考えていますが、:

TThread.WaitFor(Delphi)のような他の言語の友人は、Windows MEのような効率的なマルチスレッド化は動作していますシステム。

他のマルチスレッドの概念(プール、タスク、アプリライフタイムスレッド、プロデューサ/コンシューマキューを介したスレッド間通信)を理解してください。実際、Join()以外のほとんどすべて。

+3

こんにちはマーティン、C + +でマルチスレッドに関するお勧めの読書は? – Guagua

1

スレッドの実行が完了してjoin()が呼び出されるまで、現在のスレッドをブロックします。

スレッドでjoin()またはdettach()を指定しないと、メイン/現在のスレッドが実行を完了し、作成された他のスレッドがまだ実行されているため、実行時エラーが発生します。

関連する問題