C++ 執行緒庫 - 函式 joinable



描述

它返回執行緒物件是否可連線。

宣告

以下是 std::thread::joinable 函式的宣告。

bool joinable() const noexcept;

C++11

bool joinable() const noexcept;

引數

返回值

如果執行緒可連線則返回 true,否則返回 false。

異常

不丟擲保證 - 從不丟擲異常。

資料競爭

訪問物件。

示例

在以下 std::thread::joinable 示例中。

#include <iostream>
#include <thread>
#include <chrono>
 
void foo() {
   std::this_thread::sleep_for(std::chrono::seconds(2));
}
 
int main() {
   std::thread t;
   std::cout << "before joinable: " << t.joinable() << '\n';

   t = std::thread(foo);
   std::cout << "after joinable: " << t.joinable() << '\n';

   t.join();
   std::cout << "after joining, joinable: " << t.joinable() << '\n';
}

輸出應如下所示:

before joinable: 0
after joinable: 1
after joining, joinable: 0
thread.htm
廣告

© . All rights reserved.