C++ 執行緒庫 - join 函式



描述

執行緒執行完成後返回。

宣告

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

void join();

C++11

void join();

引數

返回值

異常

絕不丟擲異常 - 從不丟擲異常。

資料競爭

訪問物件。

示例

以下為 std::thread::join 的示例。

#include <iostream>
#include <thread>
#include <chrono>

void foo() {
   std::this_thread::sleep_for(std::chrono::seconds(1));
}

void bar() {
   std::this_thread::sleep_for(std::chrono::seconds(1));
}

int main() {
   std::cout << "starting helper...\n";
   std::thread helper1(foo);

   std::cout << "starting another helper...\n";
   std::thread helper2(bar);

   std::cout << "waiting for helpers to finish..." << std::endl;
   helper1.join();
   helper2.join();

   std::cout << "done!\n";
}

輸出應如下所示:

starting helper...
starting another helper...
waiting for helpers to finish...
done!
thread.htm
廣告
© . All rights reserved.