C++ で std::condition_variable の notify_all を用いて他の全てのスレッドを起こす2020年10月06日 12時33分10秒

condition_variable の notify_one は一つのスレッドを起こす。そして、全てのスレッドを起こせる notify_all() もある。 以下の例では、十個のスレッドを生成し condition_variable を用いて待機。メインスレッドが一秒間をおいた後に、一気に全てのスレッドを起こす。

こちらの例も、十個のスレッドを生成し condition_variable を用いて待機。メインスレッドではスレッドの終了がうまく処理できなくなるので、notify_all_at_thread_exit() を呼ぶためのスレッドを生成し、一気に全てのスレッドを起こす。

% cat condition_variable.cpp
#include <thread>
#include <condition_variable>
#include <chrono>
#include <iostream>

std::mutex m;
std::condition_variable cv;

void print( int n )
{
    std::unique_lock< std::mutex > lock( m );
    std::cout << "THREAD in  " << n << std::endl;
    cv.wait(lock);
    std::cout << "THREAD out " << n << std::endl;
}

int main()
{
    std::thread threads[ 10 ];
    for( int i = 0; i < 10; ++i )
        threads[ i ] = std::thread( print, i );

    std::this_thread::sleep_for( std::chrono::seconds( 1 ) );

    std::unique_lock< std::mutex > lock( m );
    std::cout << "notify_all()" << std::endl;
    cv.notify_all();
    lock.unlock();

    for( auto& t: threads )
        t.join();

    std::cout << "DONE" << std::endl;
}

コンパイルして実行した結果。スレッドライブラリを使うので -lptherad が必要になる。

% c++ -std=c++11 condition_variable.cpp -lpthread
% ./a.out
THREAD in  0
THREAD in  1
THREAD in  2
THREAD in  4
THREAD in  6
THREAD in  8
THREAD in  3
THREAD in  5
THREAD in  7
THREAD in  9
notify_all()
THREAD out 7
THREAD out 9
THREAD out 5
THREAD out 6
THREAD out 2
THREAD out 0
THREAD out 3
THREAD out 8
THREAD out 4
THREAD out 1
DONE
なお、実行結果はプログラムを走らせる度に変わる。

前回次回