C++ で std::condition_variable の notify_all を用いて他の全てのスレッドを起こす ― 2020年10月06日 12時33分10秒
こちらの例も、十個のスレッドを生成し 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
最近のコメント