C++ std::thread概念介紹
- 2019 年 10 月 3 日
- 筆記
C++ 11新標準中,正式的為該語言引入了多執行緒概念。新標準提供了一個執行緒庫thread,通過創建一個thread對象來管理C++程式中的多執行緒。
本文簡單聊一下C++多執行緒相關的一些概念及thread的基本用法。
0. 並行執行
程式並行執行兩個必要條件:
- 多處理器(multiple processors)or 多核處理器(multicore processors)
- 軟體並行
軟體並發執行可分為兩大類:
- 多執行緒並發 (同一個進程的多個執行緒並行);
- 多進程並發 (不同進程並行);
對於多執行緒,主要關注的是執行緒間的同步措施,用於確保執行緒安全;
對於多進程,主要關注的是進程間的通訊機制,用於進程間傳遞消息和數據;
由於C++ 標準中沒有多進程之間通訊相關的標準,這些只能依賴於特定平台的API。本文只關注多執行緒相關。
1. C++多執行緒平台
C++11之前,window和linux平台分別有各自的多執行緒標準。使用C++編寫的多執行緒往往是依賴於特定平台的。
- Window平台提供用於多執行緒創建和管理的win32 api;
- Linux下則有POSIX多執行緒標準,Threads或Pthreads庫提供的API可以在類Unix上運行;
在C++11新標準中,可以簡單通過使用hread庫,來管理多執行緒。thread庫可以看做對不同平台多執行緒API的一層包裝;
因此使用新標準提供的執行緒庫編寫的程式是跨平台的。
2. pthread 或 C++ 11 thread
pthreads 是linux下的C++執行緒庫,提供了一些執行緒相關的操作,比較偏向於底層,對執行緒的操作也是比較直接和方便的;
#include <pthread.h> pthread_create (thread, attr, start_routine, arg)
linux上對於pthread的使用需要連接pthread庫(有些編輯器可能需要 -std=c++11):
g++ source.cpp -lpthread -o source.o
儘管網上對C++ 11新標準中的thread類有很多吐槽,但是作為C++第一個標準執行緒庫,還是有一些值得肯定的地方的,比如跨平台,使用簡單。
而且新標準中可以方便的使用RAII來實現lock的管理等。
如果你想深入研究一下多執行緒,那麼pthread是一個不錯的選擇。如果想要跨平台或者實現一些簡單的多執行緒場景而不過多關注細節,那麼權威的標準庫thread是不二之選。
總之沒有好與壞之分,適合就好。可以的話可以都了解一下。本文主要介紹後者。
3. 先理論後實踐
對於多執行緒相關的學習,先弄清楚執行緒相關的一些概念,是很重要的。
比如執行緒安全、執行緒同步與互斥關係、執行緒如何通訊、與進程的關係如何等。
不然實際寫多執行緒程式是會碰到太多的問題,例如:
- 程式死鎖,無響應;
- 執行結果不符合預期;
- 多執行緒性能並沒有很大提升;
- 理不清程式執行流程;
- 不知道怎麼調試;
- 程式運行時好時壞;
光執行緒安全就有很多理論要了解,這些光靠調試程式,根據結果來猜測是不可行的。
關於多執行緒相關的概念可以參考我之前以Python為例介紹執行緒的博文:
4. thread 多執行緒實例
看一下C++11 使用標準庫thread創建多執行緒的例子:
1 #include<iostream> 2 #include<thread> 3 #include<string> 4 5 using namespace std; 6 7 int tstart(const string& tname) { 8 cout << "Thread test! " << tname << endl; 9 return 0; 10 } 11 12 int main() { 13 thread t(tstart, "C++ 11 thread!"); 14 t.join(); 15 cout << "Main Function!" << endl; 16 }
多執行緒標準庫使用一個thread的對象來管理產生的執行緒。該例子中執行緒對象t表示新建的執行緒。
4.1 標準庫創建執行緒的方式
打開thread頭文件,可以清楚的看到thread提供的構造函數。
- 默認構造函數 thread() noexcept;
- 接受函數及其傳遞參數的構造函數 template <class _Fn, class… _Args, …> explicit thread(_Fn&& _Fx, _Args&&… _Ax)
- move構造函數 thread(thread&& _Other) noexcept;
- 拷貝構造函數 thread(const thread&) = delete;
- 拷貝賦值運算符 thread& operator=(const thread&) = delete;
其中拷貝構造函數和拷貝賦值運算符被禁用,意味著std::thread對象不能夠被拷貝和賦值到別的thread對象;
默認構造函數構造一個空的thread對象,但是不表示任何執行緒;
接受參數的構造函數創建一個表示執行緒的對象,執行緒從傳入的函數開始執行,該對象是joinable的;
move構造函數可以看做將一個thread對象對執行緒的控制許可權轉移到另一個thread對象;執行之後,傳入的thread對象不表示任何執行緒;
int main() { int arg = 0; std::thread t1; // t1 is not represent a thread std::thread t2(func1, arg + 1); // pass to thread by value std::thread t3(func2, std::ref(arg)); // pass to thread by reference std::thread t4(std::move(t3)); // t4 is now running func2(). t3 is no longer a thread //t1.join() Error! t2.join(); //t3.join() Error! t4.join(); }
多數情況下我們使用的是上面第二種創建執行緒的方式。下面看一下join和detach。
4.2 join && detach
對於創建的執行緒,一般會在其銷毀前調用join和detach函數;
弄清楚這兩個函數的調用時機和意義,以及調用前後執行緒狀態的變化非常重要。
- join 會使當前執行緒阻塞,直到目標執行緒執行完畢;
- 只有處於活動狀態執行緒才能調用join,可以通過joinable()函數檢查;
- joinable() == true表示當前執行緒是活動執行緒,才可以調用join函數;
- 默認構造函數創建的對象是joinable() == false;
- join只能被調用一次,之後joinable就會變為false,表示執行緒執行完畢;
- 調用 ternimate()的執行緒必須是 joinable() == false;
- 如果執行緒不調用join()函數,即使執行完畢也是一個活動執行緒,即joinable() == true,依然可以調用join()函數;
- detach 將thread對象及其表示的執行緒分離;
- 調用detach表示thread對象和其表示的執行緒完全分離;
- 分離之後的執行緒是不在受約束和管制,會單獨執行,直到執行完畢釋放資源,可以看做是一個daemon執行緒;
- 分離之後thread對象不再表示任何執行緒;
- 分離之後joinable() == false,即使還在執行;
join實例分析:
int main() { thread t(tstart, "C++ 11 thread!"); cout << t.joinable() << endl; if (t.joinable()) t.join(); //t.detach(); Error cout << t.joinable() << endl; // t.join(); Error cout << "Main Function!" << endl; system("pause"); }
簡單來說就是只有處於活動狀態的執行緒才可以調用join,調用返回表示執行緒執行完畢,joinable() == false.
inline void thread::join() { // join thread if (!joinable()) _Throw_Cpp_error(_INVALID_ARGUMENT); const bool _Is_null = _Thr_is_null(_Thr); // Avoid Clang -Wparentheses-equality ... ... }
將上面的t.join()換成是t.detach()會得到相同的結果.
void detach() { // detach thread if (!joinable()) _Throw_Cpp_error(_INVALID_ARGUMENT); _Thrd_detachX(_Thr); _Thr_set_null(_Thr); }
上面是thread文件中對detach的定義,可以看出只有joinable() == true的執行緒,也就是活動狀態的執行緒才可以調用detach。
~thread() _NOEXCEPT { // clean up if (joinable()) _XSTD terminate(); }
當執行緒既沒有調用join也沒有調用detach的時候,執行緒執行完畢joinable() == true,那麼當thread對象被銷毀的時候,會調用terminate()。
4.3 獲取執行緒ID
執行緒ID是一個執行緒的標識符,C++標準中提供兩種方式獲取執行緒ID;
- thread_obj.get_id();
- std::this_thread::get_id()
有一點需要注意,就是空thread對象,也就是不表示任何執行緒的thread obj調用get_id返回值為0;
此外當一個執行緒被detach或者joinable() == false時,調用get_id的返回結果也為0。
cout << t.get_id() << ' ' << this_thread::get_id() << endl; //t.detach(); t.join(); cout << t.get_id() << ' ' << std::this_thread::get_id() << endl;
4.4 交換thread表示的執行緒
除了上面介紹的detach可以分離thread對象及其所表示的執行緒,或者move到別的執行緒之外,還可以使用swap來交換兩個thread對象表示的執行緒。
實例來看一下兩個執行緒的交換。
int tstart(const string& tname) { cout << "Thread test! " << tname << endl; return 0; } int main() { thread t1(tstart, "C++ 11 thread_1!"); thread t2(tstart, "C++ 11 thread_2!"); cout << "current thread id: " << this_thread::get_id() << endl; cout << "before swap: "<< " thread_1 id: " << t1.get_id() << " thread_2 id: " << t2.get_id() << endl; t1.swap(t2); cout << "after swap: " << " thread_1 id: " << t1.get_id() << " thread_2 id: " << t2.get_id() << endl; //t.detach(); t1.join(); t2.join(); }
結果:
Thread test! C++ 11 thread_1! Thread test! C++ 11 thread_2! current thread id: 39308 before swap: thread_1 id: 26240 thread_2 id: 37276 after swap: thread_1 id: 37276 thread_2 id: 26240
下面是thread::swap函數的實現。
void swap(thread& _Other) _NOEXCEPT { // swap with _Other _STD swap(_Thr, _Other._Thr); }
可以看到交換的過程僅僅是互換了thread對象所持有的底層句柄;
關於C++ 多執行緒新標準thread的基本介紹就到這裡了,看到這裡應該有一個簡單的認識了。
關於執行緒安全和管理等高級話題,後面有空在寫文章介紹。