C++ std::thread概念介紹


C++ 11新標准中,正式的為該語言引入了多線程概念。新標准提供了一個線程庫thread,通過創建一個thread對象來管理C++程序中的多線程。

本文簡單聊一下C++多線程相關的一些概念及thread的基本用法。

0. 並行執行

程序並行執行兩個必要條件:

  • 多處理器(multiple processors)or 多核處理器(multicore processors)
  • 軟件並行

軟件並發執行可分為兩大類:

  1. 多線程並發  (同一個進程的多個線程並行);
  2. 多進程並發  (不同進程並行);

對於多線程,主要關注的是線程間的同步措施,用於確保線程安全;

對於多進程,主要關注的是進程間的通信機制,用於進程間傳遞消息和數據;

由於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提供的構造函數。

  1. 默認構造函數                                         thread() noexcept; 
  2. 接受函數及其傳遞參數的構造函數      template <class _Fn, class... _Args, ...> explicit thread(_Fn&& _Fx, _Args&&... _Ax)
  3. move構造函數                                       thread(thread&& _Other) noexcept;
  4. 拷貝構造函數                                         thread(const thread&) = delete;
  5. 拷貝賦值運算符                                     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;

  1. thread_obj.get_id();
  2. 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的基本介紹就到這里了,看到這里應該有一個簡單的認識了。

關於線程安全和管理等高級話題,后面有空在寫文章介紹。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM