名前空間
変種
操作

std::jthread::joinable

提供: cppreference.com
< cpp‎ | thread‎ | jthread
 
 
スレッドサポートライブラリ
スレッド
(C++11)
(C++20)
(C++20)
this_thread 名前空間
(C++11)
(C++11)
(C++11)
相互排他
(C++11)
汎用ロック管理
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
条件変数
(C++11)
セマフォ
ラッチとバリア
(C++20)
(C++20)
フューチャー
(C++11)
(C++11)
(C++11)
(C++11)
 
 
[[nodiscard]] bool joinable() const noexcept;
(C++20以上)

std::jthread オブジェクトがアクティブなスレッドを表すかどうか調べます。 具体的には、 get_id() != std::jthread::id() であれば true を返します。 そのためデフォルト構築された jthread は合流可能ではありません。

コードの実行を終えたけれどもまだ合流していないスレッドはまだアクティブであるとみなされ、そのため合流可能です。

目次

[編集] 引数

(なし)

[編集] 戻り値

jthread オブジェクトがアクティブなスレッドを表す場合は true、そうでなければ false

[編集]

#include <iostream>
#include <thread>
#include <chrono>
 
void foo()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::jthread 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.3.2 Members [thread.jthread.mem]

[編集] 関連項目

スレッドの id を返します
(パブリックメンバ関数) [edit]
スレッドの実行終了を待ちます
(パブリックメンバ関数) [edit]
スレッドを jthread オブジェクトから独立して実行できるようにします
(パブリックメンバ関数) [edit]