std::thread::joinable
提供: cppreference.com
bool joinable() const noexcept; |
(C++11以上) | |
std::thread オブジェクトがアクティブなスレッドを表すかどうか調べます。 具体的には、 get_id() != std::thread::id() であれば true を返します。 そのためデフォルト構築された thread は合流可能ではありません。
コードの実行を終えたけれどもまだ合流していないスレッドはまだアクティブであるとみなされ、そのため合流可能です。
引数
(なし)
戻り値
thread オブジェクトがアクティブなスレッドを表す場合は true、そうでなければ false。
例
Run this code
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::thread t;
std::cout << "before starting, joinable: " << std::boolalpha << t.joinable()
<< '\n';
t = std::thread(foo);
std::cout << "after starting, joinable: " << t.joinable()
<< '\n';
t.join();
std::cout << "after joining, joinable: " << t.joinable()
<< '\n';
}
出力:
before starting, joinable: false
after starting, joinable: true
after joining, joinable: false
参考文献
- C++20 standard (ISO/IEC 14882:2020):
- 32.4.2.5 Members [thread.thread.member]
- C++17 standard (ISO/IEC 14882:2017):
- 33.3.2.5 thread members [thread.thread.member]
- C++14 standard (ISO/IEC 14882:2014):
- 30.3.1.5 thread members [thread.thread.member]
- C++11 standard (ISO/IEC 14882:2011):
- 30.3.1.5 thread members [thread.thread.member]
関連項目
| スレッドの id を返します (パブリックメンバ関数) | |
| スレッドの実行終了を待ちます (パブリックメンバ関数) | |
| スレッドを thread オブジェクトから独立して実行できるようにします (パブリックメンバ関数) |