2016-08-28 8 views
1

私は最近、マルチスレッドの周りで頭を抱えて、どのように動作するかを理解しようとしています。私はここにいくつかのサンプルコードを持っていますが、私は出力がそれがどのようなものなのか理解できません。スレッドの実行順序は何ですか?

例コード:

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

using namespace std; 


std::mutex mtx; 
int global_counter = 0; 
std::mutex counter_mutex; 

void five_thread_fn(){ 
    for(int i = 0; i<5; i++){ 
     counter_mutex.lock(); 
     global_counter++; 
     std::cout << "Updated from five_thread" << endl; 
     counter_mutex.unlock(); 
     // 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++; 
     std::cout << "Updated from ten_thread" << endl; 
     counter_mutex.unlock(); 
     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 << "Starting thread five..." << endl; 
    std::thread five_thread(five_thread_fn); 


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

    std::cout << "Ten Thread is done." << std::endl; 



    // std::cin.ignore(); 

    return 0; 


} 

私は、スレッドでの時間遅延をコメントアウトし、ここで私の出力でいます

Starting thread ten... 
Starting thread five... 
Updated from five_thread 
Updated from ten_thread 
Updated from five_thread 
Updated from five_thread 
Updated from five_thread 
Updated from five_thread 
Five Thread is done. 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Updated from ten_thread 
Ten Thread is done. 

「ten_threadから更新」ん、なぜ今、私は疑問に思って後に表示されます"five_threadから更新されました"の最初の行?主な機能には

私は

five_thread.join(); 

を行うfive_threadがその実行を完了するまで、メインスレッドを中断しないようにしていますか?

私が理解するように、five_thread.join()の後の行が(順番に)実行されるのはfive_threadの終了後です。では、ten_threadはどうやって呼び出されていますか?

main()内のコード行は連続して実行されていませんか?私の理解へ

+0

'ten_thread'はメインスレッドではありません。そのプログラムには** 3 **スレッドがあり、唯一のスレッドはメインスレッドです。 'five_thread'と' ten_thread'はお互いに同時に実行されます。 – Galik

答えて

1

five_threadの実行が完了するまで、メインスレッドは中断されませんか?

はい。しかし、AがBを待っているという事実は、何らかの形でCが独自のことをやることを止めません。スレッドに参加しても、実行の点でそのスレッドの優先順位は与えられません。その時点で、結合されるスレッドが開始になることを意味するものではありません。

同時実行手段同時実行実行。を明示的に指定しない限り、並行処理の実行の間に順序はありません。スレッドに参加すると、指定されたスレッドが完了した後に現在のスレッドが再開することのみが示されます。

+0

これで、 "ten_threadから更新されました"というメッセージが表示され、そのスレッドは1秒間の待機時間を持つため、 "1から5まで更新されました"というメッセージが1秒以内に連続して表示されます。 ten_threadは正確に停止していないので、ただ待っているだけです。つまり、five_threadと同時に "going"を意味しています。ありがとう、私は今理解しています。私は時間の遅延なくテストし、メッセージはお互いに交互にプリントアウトされます。 – Red

1

は、それがfive_threadがfive_thread.join後 ラインは、()(順次)実行することを終了した後だけです。だから、 の間にten_threadが呼び出されていますか?

このスレッドは既に開始されているためです。

std::thread ten_thread(ten_thread_fn); 

実行スレッドを開始します。 ten_thread()は何かの「中間」と呼ばれていません。これは独立した実行スレッドであり、他のすべてのスレッドと同時に実行されます。

std::threadがインスタンス化されたときにスレッドが開始され、メインスレッドがそのビジネスに従事している間に実行を継続します。

すべてjoin()は、実行スレッドが終了するまでメイン実行スレッドを一時停止します。その実行スレッドは、同時に実行されています。

ten_threadがインスタンス化されるとすぐに、ten_thread_fn()が実行されていることを意味します。

+0

私は今、実行が始まるときを理解しています。ありがとうございました! – Red

関連する問題