初めに†
- C++11 では thread がサポートされている。→ std::thread class を使う
- gcc では build 時に -pthread オプションを要求される場合がある(ビルドが通っても実行できなかった場合には、これを忘れていないか、まず疑ってみる)
std::thread†
- C++11 では、 std::thread class を用いてスレッドの生成と管理を行う。
- std::thread は、 copy 不可・move 不可 となっている。
thread の作成†
- 関数や関数オブジェクトの呼び出し演算子が仮引数を取る場合、 std::thread のコンストラクタの第2引数以降に関数や関数オブジェクトの関数呼び出し演算子へ与える実引数を渡す。
- std::thread のコンストラクタに渡された実引数は、受け取る関数が参照渡しとして宣言されていたとしても、コピーされて std::thread クラスのオブジェクト内に保存される。
- 上記コピーを避けたい場合は、実引数をムーブするか、 std::ref() 関数でくるんで渡す。
- std::ref() を使用した場合、作成した thread に参照が渡されるので、元のオブジェクトの寿命に注意すること。
- thread 内で例外が発生し、 catch されないまま thread 外に送出されると、std::terminate() が呼び出され、プログラムが強制終了する。
thread 終了を待機する†
thread を手放す†
thread の識別†
- std::thread::id std::thread::get_id() を呼ぶ
void foo( ostringstream &os ) {
os << "foo(): " << this_thread::get_id() << endl;
}
void bar( ostringstream &os ) {
os << "bar(): " << this_thread::get_id() << endl;
}
ostringstream os_foo;
ostringstream os_bar;
thread th1( foo(), ref( os_foo ) );
thread th2( bar(), ref( os_bar ) );
// それぞれの thread ID を得る
thread::id id1 = th1.get_id();
thread::id id2 = th2.get_id();
thread::id id3 = this_thread::get_id();
assert( id1 != is2 );
assert( id1 != is3 );
assert( id2 != is3 );
th1.join();
th2.join();
cout << os_foo.str();
cout << os_bar.str();
cout << "main thread : " << this_thread::get_id() << endl;
現在の thread の処理を明け渡す†
void std::yield() noexcept;
現在の thread をスリープする†
並行実行できる thread の数を取得する†
thread を排他制御する†
リソースのロックを管理する†
std::lock_guard†
#include <mutex>
std::unique_lock†
複数のリソースをロックする†
ロックせずに吐いたアクセスする†
スレッドセーフに1度だけ関数を呼び出す†
条件変数を使用する†
thread をまたいで値や例外を受け渡す†
- スレッド間で非同期に値や例外を受け渡す方法として、 std::promise / std::future が提供されている
- 以下は、 std::string をやり取りする例:
#include <iostream>
#include <thread>
#include<future>
static std::mutex g_print_mutex;
void LockPrintf( const std::string & str )
{
std::lock_guard<std::mutex> lk( g_print_mutex );
std::cout << str << std::endl;
}
void ThreadFuncA( std::promise<std::string> pr )
{
std::string sendstr ="from ThreadFuncA";
pr.set_value( sendstr );
LockPrintf( "ThreadFuncA" );
}
int main( int argc, char * argv[] )
{
std::promise<std::string> prom;
std::future<std::string> futu = prom.get_future();
std::thread th_a( ThreadFuncA, move( prom ) );
std::string future_string = "Rcv from A: " + futu.get();
LockPrintf( future_string );
th_a.join();
return( 0 );
}
std::promise --- 他のスレッドに渡す値や例外を設定する†
std::future --- 他のスレッドからセットされた値や例外を取り出す†
非同期処理をする†
スレッドローカル変数を使用する†