// 排他出力処理
mutex print_mutex;
void print( const string &s ) {
lock_guard<mutex> lk(print_mutex);
cout << s << endl;
}
void foo() {
print( "foo" );
}
void bar() {
print( "bar" );
}
// foo(), bar() をそれぞれ別の thread で実行
thread th1( foo );
thread th2( bar );
th1.join();
th2.join();