名前空間
変種

std::thread::joinable

提供: cppreference.com
 
 
スレッドサポートライブラリ
スレッド
(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)
 
 
<tbody> </tbody>
bool joinable() const noexcept;
(C++11以上)

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

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

引数

(なし)

戻り値

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

#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 を返します
(パブリックメンバ関数) [edit]
スレッドの実行終了を待ちます
(パブリックメンバ関数) [edit]
スレッドを thread オブジェクトから独立して実行できるようにします
(パブリックメンバ関数) [edit]