std::jthread:: joinable
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Observers | ||||
|
jthread::joinable
|
||||
| Operations | ||||
| Stop token handling | ||||
| Non-member functions | ||||
|
bool
joinable
(
)
const
noexcept
;
|
(C++20以降) | |
std::jthread
オブジェクトが実行中のスレッドを識別するかどうかをチェックします。具体的には、
true
を返します(
get_id
(
)
!
=
std
::
jthread
::
id
(
)
の場合)。したがって、デフォルト構築された
jthread
はjoinableではありません。
コードの実行が完了したが、まだ結合されていないスレッドは、実行中のアクティブスレッドと見なされるため、結合可能な状態です。
目次 |
パラメータ
(なし)
戻り値
true
が返されるのは、
std::jthread
オブジェクトが実行中のスレッドを識別している場合であり、
false
が返されるのはそれ以外の場合です。
例
#include <chrono> #include <iostream> #include <thread> using namespace std::chrono_literals; void foo() { std::this_thread::sleep_for(500ms); } int main() { std::cout << std::boolalpha; std::jthread t; std::cout << "before starting, joinable: " << t.joinable() << '\n'; t = std::jthread{foo}; std::cout << "after starting, joinable: " << t.joinable() << '\n'; t.join(); std::cout << "after joining, joinable: " << t.joinable() << '\n'; t = std::jthread{foo}; t.detach(); std::cout << "after detaching, joinable: " << t.joinable() << '\n'; }
出力:
before starting, joinable: false after starting, joinable: true after joining, joinable: false after detaching, joinable: false
参考文献
- C++23標準 (ISO/IEC 14882:2024):
-
- 33.4.4.3 メンバー関数 [thread.jthread.mem]
- C++20標準 (ISO/IEC 14882:2020):
-
- 32.4.3.2 メンバー [thread.jthread.mem]
関連項目
|
スレッドの
id
を返す
(public member function) |
|
|
スレッドの実行完了を待機する
(public member function) |
|
|
スレッドがスレッドハンドルから独立して実行することを許可する
(public member function) |