Namespaces
Variants

std::jthread:: joinable

From cppreference.net

Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
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)