【C++ 併發與多線程】1 std::thread id join

類 thread 表示單個執行線程。線程允許多個函數併發執行。緊接着關聯的線程對象構造,線程開始執行(爲任何 OS 調度延遲懸掛),始於作爲構造函數參數提供的頂層函數。忽略頂層函數的返回值,而且若它以拋異常終止,則調用 std::terminate 。頂層函數可以通過 std::promise 或修改共享變量(這可能要求同步,見 std::mutex 與 std::atomic )交流其返回值或異常到調用方。std::thread 對象亦可在不表示任何線程的狀態(默認構造、被移動、 detach 或 join 後),而執行線程可以不關聯到任何 thread 對象( detach 後)。沒有兩個 std::thread 對象會表示同一執行線程; std::thread 不可複製構造 (CopyConstructible) 或可複製賦值(CopyAssignable) ,儘管它可移動構造 (MoveConstructible) 且可移動賦值 (MoveAssignable) 。

 成員類

id

表示線程的 id 
(公開成員類)

成員函數

(構造函數)

構造新的 thread 對象 
(公開成員函數)

(析構函數)

析構 thread 對象,必須合併或分離底層線程 
(公開成員函數)

operator=

移動 thread 對象 
(公開成員函數)

觀察器

joinable

檢查線程是否可合併,即潛在地運行於平行環境中 
(公開成員函數)

get_id

返回線程的 id 
(公開成員函數)

native_handle

返回底層實現定義的線程句柄 
(公開成員函數)

hardware_concurrency

[靜態]

返回實現支持的併發線程數 
(公開靜態成員函數)

操作

join

等待線程完成其執行 
(公開成員函數)

detach

容許線程從線程句柄獨立開來執行 
(公開成員函數)

swap

交換二個 thread 對象 
(公開成員函數)

非成員函數

std::swap(std::thread)

(C++11)

特化 std::swap 算法 
(函數模板)

std::thread::id

std::thread::id

定義於頭文件 <thread>

   

class thread::id;

  (C++11 起)

類 thread::id 是輕量的可頻繁複制類,它作爲 std::thread 對象的唯一標識符工作。此類的實例亦可保有不表示任何線程的特殊辨別值。一旦線程結束,則 std::thread::id 的值可爲另一線程複用。此類爲用作包括有序和無序的關聯容器的關鍵而設計。


std::thread::join

void join();

  (C++11 起)

阻塞當前線程,直至 *this 所標識的線程完成其執行。*this 所標識的線程的完成同步於從 join() 的成功返回。

參數

(無)

返回值

(無)

後置條件

joinable 爲 false

異常

若錯誤發生則爲 std::system_error 。

錯誤條件

示例

#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 first helper...\n";
    std::thread helper1(foo);
 
    std::cout << "starting second helper...\n";
    std::thread helper2(bar);
 
    std::cout << "waiting for helpers to finish..." << std::endl;
    helper1.join();
    helper2.join();
 
    std::cout << "done!\n";
}


輸出:
starting first helper...
starting second helper...
waiting for helpers to finish...
done!

std::thread::detach

void detach();

  (C++11 起)

從 thread 對象分離執行的線程,允許執行獨立地持續。一旦線程退出,則釋放所有分配的資源。調用 detach 後, *this 不再佔有任何線程。

參數

(無)

返回值

(無)

後置條件

joinable 爲 false

異常

若 joinable() == false 或錯誤發生時爲 std::system_error 。

示例

#include <iostream>
#include <chrono>
#include <thread>
 
void independentThread() 
{
    std::cout << "Starting concurrent thread.\n";
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Exiting concurrent thread.\n";
}
 
void threadCaller() 
{
    std::cout << "Starting thread caller.\n";
    std::thread t(independentThread);
    t.detach();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Exiting thread caller.\n";
}
 
int main() 
{
    threadCaller();
    std::this_thread::sleep_for(std::chrono::seconds(5));
}

可能的輸出:
Starting thread caller.
Starting concurrent thread.
Exiting thread caller.
Exiting concurrent thread.

std::thread::swap

void swap( thread& other ) noexcept;

  (C++11 起)

互換二個 thread 對象的底層句柄。

參數

other - 要與之交換的 thread

返回值

(無)

示例

#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::thread t1(foo);
    std::thread t2(bar);
 
    std::cout << "thread 1 id: " << t1.get_id() << std::endl;
    std::cout << "thread 2 id: " << t2.get_id() << std::endl;
 
    std::swap(t1, t2);
 
    std::cout << "after std::swap(t1, t2):" << std::endl;
    std::cout << "thread 1 id: " << t1.get_id() << std::endl;
    std::cout << "thread 2 id: " << t2.get_id() << std::endl;
 
    t1.swap(t2);
 
    std::cout << "after t1.swap(t2):" << std::endl;
    std::cout << "thread 1 id: " << t1.get_id() << std::endl;
    std::cout << "thread 2 id: " << t2.get_id() << std::endl;
 
    t1.join();
    t2.join();
}

可能的輸出:
thread 1 id: 1892
thread 2 id: 2584
after std::swap(t1, t2):
thread 1 id: 2584
thread 2 id: 1892
after t1.swap(t2):
thread 1 id: 1892
thread 2 id: 2584

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章