名稱空間
變體
操作

std::jthread::joinable

來自 cppreference.com
< cpp‎ | thread‎ | jthread
 
 
併發支援庫
執行緒
(C++11)
(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++20)
(C++20)
期值
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危險指標
原子型別
(C++11)
(C++20)
原子型別的初始化
(C++11)(C++20 中已棄用)
(C++11)(C++20 中已棄用)
記憶體排序
(C++11)(C++26 中已棄用)
原子操作的自由函式
原子標誌的自由函式
 
 
bool joinable() const noexcept;
(C++20 起)

檢查 std::jthread 物件是否標識了一個活躍的執行執行緒。具體而言,如果 get_id() != std::jthread::id(),則返回 true。因此,預設構造的 jthread 不可 присоеди。

一個已完成程式碼執行但尚未被 присоеди 的執行緒仍被視為一個活躍的執行執行緒,因此可 присоеди。

目錄

[編輯] 引數

(無)

[編輯] 返回值

如果 std::jthread 物件標識了一個活躍的執行執行緒,則返回 true,否則返回 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
(公共成員函式) [編輯]
等待執行緒完成其執行
(公共成員函式) [編輯]
允許執行緒獨立於執行緒控制代碼執行
(公共成員函式) [編輯]