std::jthread::joinable
來自 cppreference.com
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 (公共成員函式) | |
等待執行緒完成其執行 (公共成員函式) | |
允許執行緒獨立於執行緒控制代碼執行 (公共成員函式) |