<thread>

public member function
<thread>

std::thread::joinable

bool joinable() const noexcept;
檢查是否可join
返回 thread 物件是否是*joinable*。

一個 thread 物件是*joinable*的,如果它代表一個執行執行緒。

在以下任何情況下,thread 物件*不是joinable*:
  • 如果它被*預設構造*。
  • 如果它已被移動(在*構造*另一個 thread 物件時,或*賦值給它*時)。
  • 如果它的成員 joindetach 被呼叫過。

引數



返回值

如果執行緒是*joinable*,則為 true
否則返回 false

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// example for thread::joinable
#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void mythread() 
{
  // do stuff...
}
 
int main() 
{
  std::thread foo;
  std::thread bar(mythread);

  std::cout << "Joinable after construction:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << '\n';
  std::cout << "bar: " << bar.joinable() << '\n';

  if (foo.joinable()) foo.join();
  if (bar.joinable()) bar.join();

  std::cout << "Joinable after joining:\n" << std::boolalpha;
  std::cout << "foo: " << foo.joinable() << '\n';
  std::cout << "bar: " << bar.joinable() << '\n';

  return 0;
}

輸出(3秒後)
Joinable after construction:
foo: false
bar: true
Joinable after joining:
foo: false
bar: false


資料競爭

該物件被訪問。

異常安全

無異常保證: 絕不丟擲異常。

另見