自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++11線程、鎖和條件變量

開發(fā) 后端
C++11標準使得C++開發(fā)人員能夠以一種標準的和平臺獨立的方式來編寫多線程代碼。本文一一講述了標準所支持的線程和同步機制。<thread>頭文件提供了名為thread的類(另外還包含了一些輔助類或方法),該類代表了一個執(zhí)行線程。頭文件<mutex>提供了幾種互斥量的實現(xiàn),以及對線程進行同步訪問的封裝類。

std::thread類代表了一個可執(zhí)行的線程,它來自頭文件<thread>。與其它創(chuàng)建線程的API(比如 Windows API中的CreateThread)不同的是, 它可以使用普通函數(shù)、lambda函數(shù)以及仿函數(shù)(實現(xiàn)了operator()函數(shù)的類)。另外,它還允許向線程函數(shù)傳遞任意數(shù)量的參數(shù)。

  1. #include <thread> void func()  
  2. // do some work } int main()  
  3. {  
  4.    std::thread t(func);  
  5.    t.join(); return 0;  

在上面的例子中,t是一個線程對象,函數(shù)func()運行于該線程之中。調(diào)用join函數(shù)后,該調(diào)用線程(本例中指的就是主線程)就會在join進來進行執(zhí)行的線程t結(jié)束執(zhí)行之前,一直處于阻塞狀態(tài)。如果該線程函數(shù)執(zhí)行結(jié)束后返回了一個值,該值也將被忽略。不過,該函數(shù)可以接受任意數(shù)量的參數(shù)。

  1. void func(int i, double d, const std::string& s)  
  2. {  
  3.     std::cout << i << ", " << d << ", " << s << std::endl;  
  4. int main()  
  5. {  
  6.    std::thread t(func, 1, 12.50, "sample");  
  7.    t.join(); return 0;  

盡管我們可以向線程函數(shù)傳遞任意數(shù)量的參數(shù),但是,所有的參數(shù)都是按值傳遞的。如果需要將參數(shù)按引用進行傳遞,那么就一定要象下例所示一樣,把該參數(shù)封裝到 std::ref或者std::cref之中。

  1. void func(int& a)  
  2. {  
  3.    a++;  
  4. int main()  
  5. int a = 42;  
  6.    std::thread t(func, std::ref(a));  
  7.    t.join();  
  8.    
  9.    std::cout << a << std::endl; return 0;  

上面程序打印結(jié)果為43,但要不是將a封裝到std::ref之中的話,輸出的將是42。

除join方法之外,這個線程類還提供了另外幾個方法:

  • swap: 將兩個線程對象的底層句柄進行交換
  • detatch: 允許執(zhí)行該方法的線程獨立于本線程對象的執(zhí)行而繼續(xù)執(zhí)行。脫離后的線程就再也不能執(zhí)行join了(你不能等待到它執(zhí)行結(jié)束了)
  1. <span style="font-family:'Courier New', Arial;font-size:9pt;line-height:1.5;">int</span><span style="font-family:'Courier New', Arial;font-size:9pt;line-height:1.5;"> main()</span> {  
  2.     std::thread t(funct);  
  3.     t.detach(); return 0;  

有一點非常重要,值得注意:線程函數(shù)中要是拋出了異常的話,使用通常的try-catch方式是捕獲不到該異常的。換句話說,下面這種做法行不通:

  1. try {  
  2.     std::thread t1(func);  
  3.     std::thread t2(func);  
  4.    
  5.     t1.join();  
  6.     t2.join();  
  7. catch(const std::exception& ex)  
  8. {  
  9.     std::cout << ex.what() << std::endl;  

要在線程間傳遞異常,你可以先在線程函數(shù)中捕獲它們,然后再將它們保存到一個合適的地方,隨后再讓另外一個線程從這個地方取得這些異常。

  1. std::vector<std::exception_ptr>  g_exceptions; void throw_function()  
  2. throw std::exception("something wrong happened");  
  3. void func()  
  4. try {  
  5.       throw_function();  
  6.    } catch(...)  
  7.    {  
  8.       std::lock_guard<std::mutex> lock(g_mutex);  
  9.       g_exceptions.push_back(std::current_exception());  
  10.    }  
  11. int main()  
  12. {  
  13.    g_exceptions.clear();  
  14.  
  15.    std::thread t(func);  
  16.    t.join(); for(auto& e : g_exceptions)  
  17.    { try { if(e != nullptr)  
  18.          {  
  19.             std::rethrow_exception(e);  
  20.          }  
  21.       } catch(const std::exception& e)  
  22.       {  
  23.          std::cout << e.what() << std::endl;  
  24.       }  
  25.    } return 0;  

要獲得更多關(guān)于捕獲并傳遞異常的知識,你可以閱讀在主線程中處理工作線程拋出的C++異常以及怎樣才能在線程間傳遞異常?。

在深入討論之前還有一點值得注意,頭文件<thread>里還在命名空間std::this_thread中提供了一些輔助函數(shù):

  • get_id: 返回膽怯線程的id
  • yield: 讓調(diào)度器先運行其它的線程,這在忙于等待狀態(tài)時很有用
  • sleep_for: 將當前線程置于阻塞狀態(tài),時間不少于參數(shù)所指定的時間段
  • sleep_util: 在指定的時刻來臨前,一直將當前的線程置于阻塞狀態(tài)

在上一個例子中,我需要對g_exceptions這個vector進行同步訪問,以確保同一個時刻只能有一個線程向其中壓入新元素。為了實現(xiàn)同步,我使用了一個互斥量,并在該互斥量上進行了鎖定。互斥量是一個核心的同步原語,C++11的<mutex>頭文件中包含了四種不同的互斥量。

  • mutex: 提供了核心的lock()函數(shù)和unlock()函數(shù),以及非阻塞式的try_lock()方法,該方法在互斥量不可用時會立即返回。
  • recursive_mutex: 運行在同一線程中,多次獲得同一個互斥量。
  • timed_mutex: 同第一條中的mutex類似,但它還帶來了另外兩個方法try_lock_for()try_lock_until(),分別用于在某個時間段內(nèi)或在某個時刻到來之前獲得該互斥量。
  • recursive_timed_mutex: 結(jié)合了第二條的timed_mutex和第三條的recusive_mutex。

以下所列就是一個使用std::mutex(注意其中g(shù)et_id()和sleep_for()這兩個前文所述的輔助函數(shù)的用法)的例子。

  1. #include <iostream>  
  2. #include <thread>  
  3. #include <mutex>  
  4. #include <chrono>  
  5.    
  6. std::mutex g_lock; void func()  
  7. {  
  8.     g_lock.lock();  
  9.    
  10.     std::cout << "entered thread " << std::this_thread::get_id() << std::endl;  
  11.     std::this_thread::sleep_for(std::chrono::seconds(rand() % 10));  
  12.     std::cout << "leaving thread " << std::this_thread::get_id() << std::endl;  
  13.    
  14.     g_lock.unlock();  
  15. int main()  
  16. {  
  17.     srand((unsigned int)time(0));  
  18.    
  19.     std::thread t1(func);  
  20.     std::thread t2(func);  
  21.     std::thread t3(func);  
  22.    
  23.     t1.join();  
  24.     t2.join();  
  25.     t3.join(); return 0;  

其輸出將類似如下所示:

  1. entered thread 10144 leaving thread 10144 entered thread 4188 leaving thread 4188 entered thread 3424 leaving thread 3424  

lock()和unlock()這兩個方法顧名思義,頭一個方法用來對互斥量進行加鎖,如果互斥量不可得便會處于阻塞狀態(tài);第二個方法用來對互斥量進行解鎖。

接下來的這個例子演示的是一個簡單的線程安全的容器(內(nèi)部使用的是std::vector)。這個容器具有添加單個元素的add()方法以及添加一批元素的addrange()方法,addrange()方法內(nèi)只是簡單的調(diào)用了add()方法。 

  1. template <typename T> class container   
  2. {  
  3.     std::mutex _lock;  
  4.     std::vector<T> _elements; publicvoid add(T element)   
  5.     {  
  6.         _lock.lock();  
  7.         _elements.push_back(element);  
  8.         _lock.unlock();  
  9.     } void addrange(int num, ...)  
  10.     {  
  11.         va_list arguments;  
  12.    
  13.         va_start(arguments, num); for (int i = 0; i < num; i++)  
  14.         {  
  15.             _lock.lock();  
  16.             add(va_arg(arguments, T));  
  17.             _lock.unlock();  
  18.         }  
  19.    
  20.         va_end(arguments);   
  21.     } void dump()  
  22.     {  
  23.         _lock.lock(); for(auto e : _elements)  
  24.             std::cout << e << std::endl;  
  25.         _lock.unlock();  
  26.     }  
  27. }; void func(container<int>& cont)  
  28. {  
  29.     cont.addrange(3, rand(), rand(), rand());  
  30. int main()  
  31. {  
  32.     srand((unsigned int)time(0));  
  33.    
  34.     container<int> cont;  
  35.    
  36.     std::thread t1(func, std::ref(cont));  
  37.     std::thread t2(func, std::ref(cont));  
  38.     std::thread t3(func, std::ref(cont));  
  39.    
  40.     t1.join();  
  41.     t2.join();  
  42.     t3.join();  
  43.    
  44.     cont.dump(); return 0;  

這個程序執(zhí)行起來會進入死鎖狀態(tài)。其原因在于,該容器多次嘗試獲取同一個互斥量而之前卻并沒有釋放該互斥量,這么做是行不通的。這正是std::recursive_mutex的用武之地,它允許同一個線程多次獲得同一個互斥量,可重復獲得的最大次數(shù)并未具體說明,但一旦查過一定次數(shù),再對lock進行調(diào)用就會拋出std::system錯誤。為了修復上面所列代碼的死鎖問題(不通過修改addrange方法的實現(xiàn),讓它不對lock和unlock方法進行調(diào)用),我們可以將互斥量改為std::recursive_mutex。

  1. template <typename T> class container   
  2. {  
  3.     std::recursive_mutex _lock; // ...   
  4. }; 

經(jīng)過修改之后,該程序的輸出會同如下所示類似:

  1. 6334 18467 41 6334 18467 41 6334 18467 41  

明眼的讀者可能已經(jīng)發(fā)現(xiàn)了,每次調(diào)用func()所產(chǎn)生的數(shù)字序列都完全相同。這是因為對srad的初始化是要分線程進行的,對srand()的調(diào)用只是在主線程中進行了初始化。在其它的工作線程中,srand并沒有得到初始化,所以每次產(chǎn)生的數(shù)字序列就是完全相同的了。

顯式的加鎖和解鎖可能會導致一定的問題,比如忘了解鎖或者加鎖的順序不對都有可能導致死鎖。本標準提供了幾個類和函數(shù)用于幫助解決這類問題。使用這些封裝類就能夠以相互一致的、RAII風格的方式使用互斥量了,它們可以在相應(yīng)的代碼塊的范圍內(nèi)進行自動的加鎖和解鎖動作。這些封裝類包括:

  • lock_guard: 該類的對象在構(gòu)造之時會試圖獲得互斥量的擁有權(quán)(通過調(diào)用lock()實現(xiàn)),而在析構(gòu)之時會自動釋放它所獲得的互斥量(通過調(diào)用unlock()實現(xiàn))。這是一個不可復制的類。
  • unique_lock: 是一個通用的互斥量封裝類。與lock_quard不同,它還支持延遲加鎖、時間鎖、遞歸鎖、鎖所有權(quán)的轉(zhuǎn)移并且還支持使用條件變量。這也是一個不可復制的類,但它是可以移動的類。

使用這些封裝類,我們可以象這樣來改寫我們的容器:

  1. template <typename T> class container   
  2. {  
  3.     std::recursive_mutex _lock;  
  4.     std::vector<T> _elements; publicvoid add(T element)   
  5.     {  
  6.         std::lock_guard<std::recursive_mutex> locker(_lock);  
  7.         _elements.push_back(element);  
  8.     } void addrange(int num, ...)  
  9.     {  
  10.         va_list arguments;  
  11.    
  12.         va_start(arguments, num); for (int i = 0; i < num; i++)  
  13.         {  
  14.             std::lock_guard<std::recursive_mutex> locker(_lock);  
  15.             add(va_arg(arguments, T));  
  16.         }  
  17.    
  18.         va_end(arguments);   
  19.     } void dump()  
  20.     {  
  21.         std::lock_guard<std::recursive_mutex> locker(_lock); for(auto e : _elements)  
  22.             std::cout << e << std::endl;  
  23.     }  
  24. }; 

有人會說,既然dump()方法并不會對容器的狀態(tài)做出任何修改,所以它應(yīng)該定義為congst的方法。但要是你真的這么改了之后,編譯器就會報告出如下的錯誤:

  1. ‘std::lock_guard<_Mutex>::lock_guard(_Mutex &)' : cannot convert parameter 1 from ‘const std::recursive_mutex' to ‘std::recursive_mutex &'  

互斥量(無論使用的是哪一種實現(xiàn))必須要獲得和釋放,這就意味著要調(diào)用非常量型的lock()和unlock()方法。所以,從邏輯上講,lock_guard不能在定義中添加const(因為該方法定義為const的話,互斥量也就必需是const的了)這個問題有個解決辦法,可以讓 mutex變?yōu)閙utable的。成為 mutable之后就可以在const函數(shù)中對狀態(tài)進行修改了。不過,這種用法應(yīng)該只用于隱藏的或者“元”狀態(tài)(比如,對計算結(jié)果或者查詢到的數(shù)據(jù)進行緩存,以供下次調(diào)用時直接使用而無需再次計算或查詢;再比如,對 只是對對象的實際狀態(tài)起著輔助作用的互斥量中的位進行修改)。

  1. template <typename T> class container   
  2. {  
  3.    mutable std::recursive_mutex _lock;  
  4.    std::vector<T> _elements; publicvoid dump() const {  
  5.       std::lock_guard<std::recursive_mutex> locker(_lock); for(auto e : _elements)  
  6.          std::cout << e << std::endl;  
  7.    }  
  8. }; 

這些封裝類都具有可以接受一個用來指導加鎖策略的參數(shù)的構(gòu)造器,可用的加鎖策略有:

  • defer_lockof typedefer_lock_t: 不要取得互斥量的擁有權(quán)
  • try_to_lockof typetry_to_lock_t: 在不會被阻塞的情況下嘗試獲得互斥量的擁有權(quán)
  • adopt_lockof typeadopt_lock_t: 假設(shè)調(diào)用線程已經(jīng)獲得了互斥量的擁有權(quán)

這些策略的定義如下所示:

  1. struct defer_lock_t { };   
  2. struct try_to_lock_t { };   
  3. struct adopt_lock_t { };   
  4. constexpr std::defer_lock_t defer_lock = std::defer_lock_t();   
  5. constexpr std::try_to_lock_t try_to_lock = std::try_to_lock_t();   
  6. constexpr std::adopt_lock_t adopt_lock = std::adopt_lock_t(); 

除了這些互斥量的封裝類,本標準還提供了幾個用來對一個或多個互斥量進行加鎖的方法。

  • lock: 使用一種可避免死鎖的算法對互斥量進行加鎖(通過調(diào)用tolock()、try_lock()以及unlock())。
  • try_lock: 通過調(diào)用try_lock()i按照參數(shù)里指定的互斥量的順序?qū)Χ鄠€互斥量進行加鎖。

這里舉一個造成死鎖的例子:我們有一個保存元素的容器,還有一個叫做exchange()的方法,用來將一個元素從一個容器中取出來放入另外一個容器。為了成為線程安全的函數(shù),這個函數(shù)通過獲得每個容器的互斥量,對兩個容器的訪問進行了同步處理。

  1. template <typename T> class container   
  2. public:  
  3.     std::mutex _lock;  
  4.     std::set<T> _elements; void add(T element)   
  5.     {  
  6.         _elements.insert(element);  
  7.     } void remove(T element)   
  8.     {  
  9.         _elements.erase(element);  
  10.     }  
  11. }; void exchange(container<int>& cont1, container<int>& cont2, int value)  
  12. {  
  13.     cont1._lock.lock();  
  14.     std::this_thread::sleep_for(std::chrono::seconds(1)); // <-- forces context switch to simulate the deadlock  cont2._lock.lock();      
  15.    
  16.     cont1.remove(value);  
  17.     cont2.add(value);  
  18.    
  19.     cont1._lock.unlock();  
  20.     cont2._lock.unlock();  

假設(shè)這個函數(shù)是從兩個不同的線程中進行調(diào)用的,在第一個線程中有一個元素從第一個容器中取出來,放到了第二個容器中,在第二個線程中該元素又從第二個容器中取出來放回到了第一個容器中。這樣會導致死鎖(如果線程上下文正好在獲得第一個鎖的時候從一個線程切換到了另一個線程的時候就會發(fā)生死鎖)。

  1. int main()  
  2. {  
  3.     srand((unsigned int)time(NULL));  
  4.    
  5.     container<int> cont1;   
  6.     cont1.add(1);  
  7.     cont1.add(2);  
  8.     cont1.add(3);  
  9.    
  10.     container<int> cont2;   
  11.     cont2.add(4);  
  12.     cont2.add(5);  
  13.     cont2.add(6);  
  14.    
  15.     std::thread t1(exchange, std::ref(cont1), std::ref(cont2), 3);  
  16.     std::thread t2(exchange, std::ref(cont2), std::ref(cont1), 6);  
  17.    
  18.     t1.join();  
  19.     t2.join(); return 0;  

要解決該問題,你可以使用以能夠避免死鎖的方式獲得鎖的std::lock:

  1. void exchange(container<int>& cont1, container<int>& cont2, int value)  
  2. {  
  3.     std::lock(cont1._lock, cont2._lock);   
  4.    
  5.     cont1.remove(value);  
  6.     cont2.add(value);  
  7.    
  8.     cont1._lock.unlock();  
  9.     cont2._lock.unlock();  

#p#

條件變量

C++11還提供了對另外一個同步原語的支持,這個原語就是條件變量。使用條件變量可以將一個或多個線程進入阻塞狀態(tài),直到收到另外一個線程的通知,或者超時或者發(fā)生了虛假喚醒,才能退出阻塞狀態(tài)。頭文件<condition_variable>中包含的條件變量有兩種實現(xiàn):

  • condition_variable: 要求任何想等待該條件變量的線程必需先獲得std::unique_lock鎖。
  • condition_variable_any: 該實現(xiàn)更加通用,它可以用于任何滿足基本條件的鎖(只要實現(xiàn)了lock()和unlock()方法即可)。因為它使用起來代價要更高一些(從性能和操作系統(tǒng)的字樣的角度講),所以,應(yīng)該在只有它所提供的額外的靈活性是必不可少的情況下才會選用它。

下面說說條件變量的工作原理:

  • 必須至少要有一個等待條件變?yōu)閠rue的線程。等待中的線程必須首先獲得一個unique_lock鎖。 該鎖將會傳遞給wait()方法,然后wait()方法會釋放互斥量并將該線程暫停,直到條件變量得到相應(yīng)的信號。當接受到信號,線程被喚醒后,該鎖就又被重新獲得了。
  • 必須至少要有一個線程發(fā)送信號使得條件變?yōu)閠rue。信號可以通過調(diào)用notify_one()來發(fā)送,發(fā)用這個方法發(fā)送后就會將處于阻塞狀態(tài)的等待該條件獲得信號的線程中的某一個線程(任意一個線程)恢復執(zhí)行;還可以通過調(diào)用notify_all()將等待該條件的所以線程喚醒。
  • 因為在多處理器的環(huán)境下,要讓條件喚醒成為完全可預測會有一些復雜情況難以克服,所以就會出現(xiàn)一些虛假喚醒。也就是說,線程甚至在沒有人向條件變量發(fā)送信號的情況下就有可能會被喚醒。因此,在線程喚醒后,仍然需要檢測條件是不是還為true。而且因為虛假喚醒可能會多次發(fā)生,所以該檢測必須用一個循環(huán)來進行。

以下代碼給出了一個利用狀態(tài)變量來同步線程的例子:幾個工作線程可能在他們運行的時候產(chǎn)生錯誤并且他們把這些錯誤放到隊列里面。一個記錄線程會通過從隊列得到并輸出錯誤來處理這些錯誤代碼。當有錯誤發(fā)生的時候,工作線程會發(fā)信號給記錄線程。記錄線程一直在等待著狀態(tài)變量接收信號。為了防止虛假的喚醒,所以記錄線程的等待是發(fā)生在一個以檢測布爾值(boolean)的循環(huán)之中的。

  1. #include <thread>  
  2. #include <mutex>  
  3. #include <condition_variable>  
  4. #include <iostream>  
  5. #include <queue>  
  6. #include <random>  
  7.  
  8. std::mutex              g_lockprint;  
  9. std::mutex              g_lockqueue;  
  10. std::condition_variable g_queuecheck;  
  11. std::queue<int>         g_codes; bool g_done; bool g_notified; void workerfunc(int id, std::mt19937& generator)  
  12. // print a starting message  {  
  13.         std::unique_lock<std::mutex> locker(g_lockprint);  
  14.         std::cout << "[worker " << id << "]\trunning..." << std::endl;  
  15.     } // simulate work  std::this_thread::sleep_for(std::chrono::seconds(1 + generator() % 5)); // simulate error  int errorcode = id*100+1;  
  16.     {  
  17.         std::unique_lock<std::mutex> locker(g_lockprint);  
  18.         std::cout  << "[worker " << id << "]\tan error occurred: " << errorcode << std::endl;  
  19.     } // notify error to be logged  {  
  20.         std::unique_lock<std::mutex> locker(g_lockqueue);  
  21.         g_codes.push(errorcode);  
  22.         g_notified = true;  
  23.         g_queuecheck.notify_one();  
  24.     }  
  25. void loggerfunc()  
  26. // print a starting message  {  
  27.         std::unique_lock<std::mutex> locker(g_lockprint);  
  28.         std::cout << "[logger]\trunning..." << std::endl;  
  29.     } // loop until end is signaled  while(!g_done)  
  30.     {  
  31.         std::unique_lock<std::mutex> locker(g_lockqueue); while(!g_notified) // used to avoid spurious wakeups  {  
  32.             g_queuecheck.wait(locker);  
  33.         } // if there are error codes in the queue process them  while(!g_codes.empty())  
  34.         {  
  35.             std::unique_lock<std::mutex> locker(g_lockprint);  
  36.             std::cout << "[logger]\tprocessing error:  " << g_codes.front()  << std::endl;  
  37.             g_codes.pop();  
  38.         }  
  39.  
  40.         g_notified = false;  
  41.     }  
  42. int main()  
  43. // initialize a random generator  std::mt19937 generator((unsigned int)std::chrono::system_clock::now().time_since_epoch().count()); // start the logger  std::thread loggerthread(loggerfunc); // start the working threads  std::vector<std::thread> threads; for(int i = 0; i < 5; ++i)  
  44.     {  
  45.         threads.push_back(std::thread(workerfunc, i+1, std::ref(generator)));  
  46.     } // work for the workers to finish  for(auto& t : threads)  
  47.         t.join(); // notify the logger to finish and wait for it  g_done = true;  
  48.     loggerthread.join(); return 0;  
  49. }  
  50. Running this code produces an output that looks like this (notice this output is different with each run because each worker thread works, i.e. sleeps, for a random interval):  
  51. [logger]        running...  
  52. [worker 1]      running...  
  53. [worker 2]      running...  
  54. [worker 3]      running...  
  55. [worker 4]      running...  
  56. [worker 5]      running...  
  57. [worker 1]      an error occurred: 101 [worker 2]      an error occurred: 201 [logger]        processing error: 101 [logger]        processing error: 201 [worker 5]      an error occurred: 501 [logger]        processing error: 501 [worker 3]      an error occurred: 301 [worker 4]      an error occurred: 401 [logger]        processing error: 301 [logger]        processing error: 401  

如上所示的wait()方法有兩個重載:

1.一個是只有一個唯一鎖;這個重載釋放鎖,封鎖線程和把線程加入都是等待這一個狀態(tài)變量的線程隊列里面;當狀態(tài)變量被信號通知后或者是一個假喚醒發(fā)生,這些線程就會被喚醒。但他們中任何一個發(fā)生時,鎖就被重新獲得然后函數(shù)返回。

2.另外一個是對于唯一鎖的添加,它也是使用一個循環(huán)的謂語直到它返回false;這個重載可以用來防止假式喚醒。它基本上是與以下是等價的:

  1. while(!predicate())   
  2.    wait(lock); 

因此在上例中,通過使用重載的wait函數(shù)以及一個驗證隊列狀態(tài)(空或不空)的斷言,就可以避免使用布爾變量g_notified了:

  1. void workerfunc(int id, std::mt19937& generator)  
  2. // print a starting message  {  
  3.         std::unique_lock<std::mutex> locker(g_lockprint);  
  4.         std::cout << "[worker " << id << "]\trunning..." << std::endl;  
  5.     } // simulate work  std::this_thread::sleep_for(std::chrono::seconds(1 + generator() % 5)); // simulate error  int errorcode = id*100+1;  
  6.     {  
  7.         std::unique_lock<std::mutex> locker(g_lockprint);  
  8.         std::cout << "[worker " << id << "]\tan error occurred: " << errorcode << std::endl;  
  9.     } // notify error to be logged  {  
  10.         std::unique_lock<std::mutex> locker(g_lockqueue);  
  11.         g_codes.push(errorcode);  
  12.         g_queuecheck.notify_one();  
  13.     }  
  14. void loggerfunc()  
  15. // print a starting message  {  
  16.         std::unique_lock<std::mutex> locker(g_lockprint);  
  17.         std::cout << "[logger]\trunning..." << std::endl;  
  18.     } // loop until end is signaled  while(!g_done)  
  19.     {  
  20.         std::unique_lock<std::mutex> locker(g_lockqueue);  
  21.  
  22.         g_queuecheck.wait(locker, [&](){return !g_codes.empty();}); // if there are error codes in the queue process them  while(!g_codes.empty())  
  23.         {  
  24.             std::unique_lock<std::mutex> locker(g_lockprint);  
  25.             std::cout << "[logger]\tprocessing error:  " << g_codes.front() << std::endl;  
  26.             g_codes.pop();  
  27.         }  
  28.     }  

除了這個wait()重載方法,還有另外兩個進行類似重載的等待方法,都有用了一個用來避免虛假喚醒的斷言:

  • wait_for: 在條件變量收到信號或者指定的超時發(fā)生前,一直都將線程置于阻塞狀態(tài)。
  • wait_until: 在條件變量收到信號或者指定的時刻到來前,一直都將線程處于阻塞狀態(tài)。

這兩個函數(shù)不帶斷言的重載函數(shù)會返回一個cv_status狀態(tài),該狀態(tài)用來表明線程被喚醒了到底是因為發(fā)生了超時還是因為條件變量收到了信號抑或是發(fā)生了虛假喚醒。

本標準還提供了一個叫做notified_all_at_thread_exit的函數(shù),它實現(xiàn)了一種機制,在該機制下,我們可以通知其它線程,某個給定的線程執(zhí)行結(jié)束了,并銷毀了所有的thread_local對象。之所以引入該函數(shù),是因為如果使用了thread_local后,采用join()之外的機制等待線程可能會導致不正確甚至是致命的行為,出現(xiàn)這樣的問題是因為 thread_local的析構(gòu)函數(shù)甚至可能會在原本處于等待中的線程繼續(xù)執(zhí)行后被執(zhí)行了而且還可能已經(jīng)執(zhí)行完成了。(有關(guān)這方面更多的情況可參見N3070N2880)。 一般情況下,notified_all_at_thread_exitTypically必須正好在線程生成前調(diào)用。下面給出一個例子,演示一下 notify_all_at_thread_exit是如何同condition_variable一起使用來對兩個線程進行同步處理的:

  1. std::mutex              g_lockprint;  
  2. std::mutex              g_lock;  
  3. std::condition_variable g_signal; bool g_done; void workerfunc(std::mt19937& generator)  
  4. {  
  5.    {  
  6.       std::unique_lock<std::mutex> locker(g_lockprint);  
  7.       std::cout << "worker running..." << std::endl;  
  8.    }  
  9.  
  10.    std::this_thread::sleep_for(std::chrono::seconds(1 + generator() % 5));  
  11.  
  12.    {  
  13.       std::unique_lock<std::mutex> locker(g_lockprint);  
  14.       std::cout << "worker finished..." << std::endl;  
  15.    }  
  16.  
  17.    std::unique_lock<std::mutex> lock(g_lock);  
  18.    g_done = true;  
  19.    std::notify_all_at_thread_exit(g_signal, std::move(lock));  
  20. int main()  
  21. // initialize a random generator  std::mt19937 generator((unsigned int)std::chrono::system_clock::now().time_since_epoch().count());  
  22.  
  23.    std::cout << "main running..." << std::endl;  
  24.  
  25.    std::thread worker(workerfunc, std::ref(generator));  
  26.    worker.detach();  
  27.  
  28.    std::cout << "main crunching..." << std::endl;  
  29.  
  30.    std::this_thread::sleep_for(std::chrono::seconds(1 + generator() % 5));  
  31.  
  32.    {  
  33.       std::unique_lock<std::mutex> locker(g_lockprint);  
  34.       std::cout << "main waiting for worker..." << std::endl;  
  35.    }  
  36.  
  37.    std::unique_lock<std::mutex> lock(g_lock); while(!g_done) // avoid spurious wake-ups  g_signal.wait(lock);  
  38.  
  39.    std::cout << "main finished..." << std::endl; return 0;  

如果工作線程是在主線程結(jié)束之前結(jié)束的,輸出將會是如下所示:

  1. main running...  
  2. worker running...  
  3. main crunching...  
  4. worker finished...  
  5. main waiting for worker...  
  6. main finished... 

如果是主線程在工作線程結(jié)束之前結(jié)束的,輸出將會是如下所示:

  1. main running...  
  2. worker running...  
  3. main crunching...  
  4. main waiting for worker...  
  5. worker finished...  
  6. main finished... 

結(jié)束語

C++11標準使得C++開發(fā)人員能夠以一種標準的和平臺獨立的方式來編寫多線程代碼。本文一一講述了標準所支持的線程和同步機制。<thread>頭文件提供了名為thread的類(另外還包含了一些輔助類或方法),該類代表了一個執(zhí)行線程。頭文件<mutex>提供了幾種互斥量的實現(xiàn),以及對線程進行同步訪問的封裝類。頭文件<condition_variable>為條件變量提供了兩種實現(xiàn),利用這些實現(xiàn)可以讓一個或多個線程進入阻塞狀態(tài),直到從收到來自另外一個或多個線程的通知、或者發(fā)生超時或虛假喚醒為止才會被喚醒。推薦在這方面再閱讀一些別的資料來獲得更詳細的信息。

英文原文:C++11 threads, locks and condition variables

譯文鏈接:http://www.oschina.net/translate/cplusplus11-threads-locks-and-condition-variables

 

責任編輯:林師授 來源: OSCHINA編譯
相關(guān)推薦

2013-07-31 11:09:05

C++11

2020-09-23 16:31:38

C++C++11啟動線程

2012-12-25 10:52:23

IBMdW

2020-06-01 21:07:33

C11C++11內(nèi)存

2024-05-29 13:21:21

2016-11-23 16:08:24

Python處理器分布式系統(tǒng)

2024-02-21 23:43:11

C++11C++開發(fā)

2013-09-25 14:20:46

2011-10-13 10:21:01

C++

2013-12-23 09:48:43

C++鎖定模式

2013-11-29 09:51:26

C++雙重檢查鎖定

2025-01-21 08:02:03

2020-12-09 10:55:25

ArrayvectorLinux

2013-12-11 10:00:14

C++新特性C

2021-06-11 10:53:40

Folly組件開發(fā)

2023-09-22 22:27:54

autoC++11

2019-04-12 15:14:44

Python線程

2025-04-30 10:10:00

在 C++C++11Lambda

2011-08-19 09:41:56

C++

2024-07-05 08:32:36

點贊
收藏

51CTO技術(shù)棧公眾號