C++ 11新標準中,正式的爲該語言引入了多線程概念。新標準提供了一個線程庫thread,經過建立一個thread對象來管理C++程序中的多線程。html
本文簡單聊一下C++多線程相關的一些概念及thread的基本用法。python
程序並行執行兩個必要條件:linux
軟件併發執行可分爲兩大類:ios
對於多線程,主要關注的是線程間的同步措施,用於確保線程安全;c++
對於多進程,主要關注的是進程間的通訊機制,用於進程間傳遞消息和數據;api
因爲C++ 標準中沒有多進程之間通訊相關的標準,這些只能依賴於特定平臺的API。本文只關注多線程相關。安全
C++11以前,window和linux平臺分別有各自的多線程標準。使用C++編寫的多線程每每是依賴於特定平臺的。多線程
在C++11新標準中,能夠簡單經過使用hread庫,來管理多線程。thread庫能夠看作對不一樣平臺多線程API的一層包裝;併發
所以使用新標準提供的線程庫編寫的程序是跨平臺的。編輯器
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是不二之選。
總之沒有好與壞之分,適合就好。能夠的話能夠都瞭解一下。本文主要介紹後者。
對於多線程相關的學習,先弄清楚線程相關的一些概念,是很重要的。
好比線程安全、線程同步與互斥關係、線程如何通訊、與進程的關係如何等。
否則實際寫多線程程序是會碰到太多的問題,例如:
光線程安全就有不少理論要了解,這些光靠調試程序,根據結果來猜想是不可行的。
關於多線程相關的概念能夠參考我以前以Python爲例介紹線程的博文:
看一下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表示新建的線程。
打開thread頭文件,能夠清楚的看到thread提供的構造函數。
其中拷貝構造函數和拷貝賦值運算符被禁用,意味着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。
對於建立的線程,通常會在其銷燬前調用join和detach函數;
弄清楚這兩個函數的調用時機和意義,以及調用先後線程狀態的變化很是重要。
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()。
線程ID是一個線程的標識符,C++標準中提供兩種方式獲取線程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;
除了上面介紹的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的基本介紹就到這裏了,看到這裏應該有一個簡單的認識了。
關於線程安全和管理等高級話題,後面有空在寫文章介紹。