成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發(fā) 后端
C++11標(biāo)準(zhǔn)可以讓C++開發(fā)者以一種標(biāo)準(zhǔn)的,獨(dú)立平臺(tái)的方式來(lái)編寫多線程。這篇文章大概講述了該標(biāo)準(zhǔn)所支持的線程和同步機(jī)制。

線程

類std::thread代表一個(gè)可執(zhí)行線程,使用時(shí)必須包含頭文件<thread>。std::thread可以和普通函數(shù),匿名函數(shù)和仿函數(shù)(一個(gè)實(shí)現(xiàn)了operator()函數(shù)的類)一同使用。另外,它允許向線程函數(shù)傳遞任意數(shù)量的參數(shù)。

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

上例中,t 是一個(gè)線程對(duì)象,函數(shù)func()運(yùn)行于該線程中。對(duì)join()函數(shù)的調(diào)用將使調(diào)用線程(本例是指主線程)一直處于阻塞狀態(tài),直到正在執(zhí)行的線程t執(zhí)行結(jié)束。如果線程函數(shù)返回某個(gè)值,該值也將被忽略。不過(guò),該函數(shù)可以接收任意數(shù)量的參數(shù)。

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

盡管可以向線程函數(shù)傳遞任意數(shù)量的參數(shù),但是所有的參數(shù)應(yīng)當(dāng)按值傳遞。如果需要將參數(shù)按引用傳遞,那要向下例所示那樣,必須將參數(shù)用std::ref 或者std::cref進(jìn)行封裝。

  1. void func(int& a) 
  2.    a++; 
  3.   
  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; 
  10.   
  11.    return 0; 

該程序打印結(jié)果為43,但是如果不用std::ref把參數(shù)a進(jìn)行封裝的話,輸出結(jié)果將為42.

除了join方法外,該線程類還提供了另外兩個(gè)方法:

swap:交換兩個(gè)線程對(duì)象的底層句柄。

Detach: 允許執(zhí)行該方法的線程脫離其線程對(duì)象而繼續(xù)獨(dú)立執(zhí)行。脫離后的線程不再是可結(jié)合線程(你不能等待它們執(zhí)行結(jié)束)。

  1. int main() 
  2.     std::thread t(funct); 
  3.     t.detach(); 
  4.   
  5.     return 0; 

有一點(diǎn)非常重要,如果線程函數(shù)拋出異常,使用常規(guī)的try-catch語(yǔ)句是捕獲不到該異常的。換句話說(shuō),以下的做法是不可行的:

  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.     std::cout << ex.what() << std::endl; 

要在線程間傳遞異常,你需要在線程函數(shù)中捕獲他們,將其存儲(chǔ)在合適的地方,比便于另外的線程可以隨后獲取到這些異常。

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

想要知道更多的關(guān)于捕獲和傳遞異常的知識(shí),可以閱讀這兩本書在主線程中處理輔助線程拋出的C++異常怎樣在線程間傳遞異常

在深入學(xué)習(xí)之前,有一點(diǎn)需要注意 &lt;thread&gt;頭文件在命名空間std::this_thread中提供了一些幫助函數(shù):

  • get_id: 返回當(dāng)前線程的id.
  • yield:在處于等待狀態(tài)時(shí),可以讓調(diào)度器先運(yùn)行其他可用的線程。
  • sleep_for:阻塞當(dāng)前線程,時(shí)間不少于其參數(shù)指定的時(shí)間。
  • sleep_util:在參數(shù)指定的時(shí)間到達(dá)之前,使當(dāng)前線程一直處于阻塞狀態(tài)。

#p#

在上面的例子中,我需要對(duì)vector g_exceptions進(jìn)行同步訪問(wèn),以確保在同一時(shí)間只能有一個(gè)線程向其中添加新元素。為此,我使用了互斥量,并對(duì)該互斥進(jìn)行加鎖。互斥量是一個(gè)核心 同步原語(yǔ),C++ 11的<mutex>頭文件里包含了四種不同的互斥量。

  • Mutex: 提供了核心函數(shù) lock() 和 unlock(),以及非阻塞方法的try_lock()方法,一旦互斥量不可用,該方法會(huì)立即返回。
  • Recursive_mutex:允許在同一個(gè)線程中對(duì)一個(gè)互斥量的多次請(qǐng)求。
  • Timed_mutex:同上面的mutex類似,但它還有另外兩個(gè)方法 try_lock_for() 和 try_lock_until(),分別用于在某個(gè)時(shí)間段里或者某個(gè)時(shí)刻到達(dá)之間獲取該互斥量。
  • Recursive_timed_mutex: 結(jié)合了timed_mutex 和recuseive_mutex的使用。

下面是一個(gè)使用了std::mutex的例子(注意前面提到過(guò)的幫助函數(shù)get_id()和sleep_for()的用法)。

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

輸出結(jié)果如下所示:

  1. entered thread 10144 
  2. leaving thread 10144 
  3. entered thread 4188 
  4. leaving thread 4188 
  5. entered thread 3424 
  6. leaving thread 3424 

lock()和unlock()這兩個(gè)方法應(yīng)該一目了然,***個(gè)方法用來(lái)對(duì)互斥量加鎖,如果互斥量不可用,便處于阻塞狀態(tài)。后者則用來(lái)對(duì)互斥量解鎖。

下面這個(gè)例子展示了一個(gè)簡(jiǎn)單的線程安全容器(內(nèi)部使用std::vector).這個(gè)容器帶有添加單個(gè)元素的add()方法和添加多個(gè)元素的addrange()方法,addrange()方法內(nèi)部?jī)H僅調(diào)用了add()方法。

注意:就像下面的評(píng)論里所指出的一樣,由于某些原因,包括使用了va_args,這不是一個(gè)標(biāo)準(zhǔn)的線程安全容器。而且,dump()方法也不是容器 的方法,從真正的實(shí)現(xiàn)上來(lái)說(shuō),它只是一個(gè)幫助(獨(dú)立的)函數(shù)。這個(gè)例子僅僅用來(lái)告訴大家一些有關(guān)互斥量的概念,而不是實(shí)現(xiàn)一個(gè)完全成熟的,無(wú)任何錯(cuò)誤的線 程安全容器。

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

運(yùn)行該程序時(shí),會(huì)進(jìn)入死鎖狀態(tài)。原因是該容器試圖多次去獲取同一個(gè)互斥量,卻一直沒(méi)有釋放它,這樣是不可行的。

在這里,使用std::recursive_mutex就可以很好地解決這個(gè)問(wèn)題,它允許同一個(gè)線程多次獲取同一個(gè)互斥量,可獲取的互斥量的***次數(shù)并沒(méi)有具體說(shuō)明。但是一旦超過(guò)***次數(shù),再對(duì)lock進(jìn)行調(diào)用就會(huì)拋出std::system_error錯(cuò)誤異常。

#p#

要想修改上述代碼中的問(wèn)題(除了修改addrange()方法的實(shí)現(xiàn),使它不去調(diào)用lock()和unlock()),還可以將互斥量std::mutex改為std::recursive_mutex

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

修改后,就會(huì)得到下面的輸出結(jié)果。

  1. 6334 
  2. 18467 
  3. 41 
  4. 6334 
  5. 18467 
  6. 41 
  7. 6334 
  8. 18467 
  9. 41 

聰明的讀者會(huì)注意到每次調(diào)用func()都會(huì)產(chǎn)生相同的數(shù)字序列。這是因?yàn)榉N子數(shù)是線程本地化的,僅僅在主線程中調(diào)用了srand()對(duì)種子進(jìn)行了初始化,在其他工作線程中并沒(méi)用進(jìn)行初始化,所以每次都得到相同的數(shù)字序列。

顯式的加鎖和解鎖會(huì)導(dǎo)致一些問(wèn)題,比如忘記解鎖或者請(qǐng)求加鎖的順序不正確,進(jìn)而產(chǎn)生死鎖。該標(biāo)準(zhǔn)提供了一些類和函數(shù)幫助解決此類問(wèn)題。這些封裝類保證了在RAII風(fēng)格上互斥量使用的一致性,可以在給定的代碼范圍內(nèi)自動(dòng)加鎖和解鎖。封裝類包括:

Lock_guard:在構(gòu)造對(duì)象時(shí),它試圖去獲取互斥量的所有權(quán)(通過(guò)調(diào)用lock()),在析構(gòu)對(duì)象時(shí),自動(dòng)釋放互斥量(通過(guò)調(diào)用unlock()).這是一個(gè)***的類。

Unique_lock:這個(gè)一通用的互斥量封裝類,不同于lock_guard,它還支持延遲加鎖,時(shí)間加鎖和遞歸加鎖以及鎖所有權(quán)的轉(zhuǎn)移和條件變量的使用。這也是一個(gè)***的類,但它是可移動(dòng)類。

有了這些封裝類,我們可以像下面這樣改寫容器類:

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

#p#

有人也許會(huì)問(wèn),既然dump()方法并沒(méi)有對(duì)容器的狀態(tài)做任何修改,是不是應(yīng)該定義為const方法呢?但是你如果將它定義為const,編譯器會(huì)報(bào)出下面的錯(cuò)誤:

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

一個(gè)互斥量(不管使用的哪一種實(shí)現(xiàn))必須要獲取和釋放,這就意味著要調(diào)用非const的lock()和unlock()方法。所以從邏輯上來(lái) 講,lock_guard的參數(shù)不能使const(因?yàn)槿绻摲椒閏onst,互斥量也必需是const).解決這個(gè)問(wèn)題的辦法就是將互斥量定義為可變 的mutable,Mutable允許在常函數(shù)中修改狀態(tài)。

不過(guò),這種方法只能用于隱藏或者元狀態(tài)(就像對(duì)計(jì)算結(jié)果或查詢的數(shù)據(jù)進(jìn)行緩存,以便下次調(diào)用時(shí)可以直接使用,不需要進(jìn)行多次計(jì)算和查詢。再或者,對(duì)在一個(gè)對(duì)象的實(shí)際狀態(tài)起輔助作用的互斥量進(jìn)行位的修改)。

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

這些封裝類的構(gòu)造函數(shù)可以重載,接受一個(gè)參數(shù)用來(lái)指明加鎖策略。可用的策略如下:

  • defer_lock of type defer_lock_t:不獲取互斥量的擁有權(quán)
  • try_to_lock of type try_to_lock_t:在不阻塞的情況下試圖獲取互斥量的擁有權(quán)
  • adopte_lock of type adopt_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.   
  5. constexpr std::defer_lock_t defer_lock = std::defer_lock_t(); 
  6. constexpr std::try_to_lock_t try_to_lock = std::try_to_lock_t(); 
  7. constexpr std::adopt_lock_t adopt_lock = std::adopt_lock_t(); 

除了這些互斥量的封裝類,該標(biāo)準(zhǔn)還提供了兩個(gè)方法,用于對(duì)一個(gè)或多個(gè)互斥量進(jìn)行加鎖。

  • lock:使用一種可以避免死鎖的算法對(duì)互斥量加鎖(通過(guò)調(diào)用lock(),try_lock()和unlock()).
  • try_lock():按照互斥量被指定的順序,試著通過(guò)調(diào)用try_lock()來(lái)對(duì)多個(gè)互斥量加鎖。

這是一個(gè)發(fā)生死鎖的例子:有一個(gè)用來(lái)存儲(chǔ)元素的容器和一個(gè)函數(shù)exchange(),該函數(shù)用來(lái)交換兩個(gè)容器中的元素。要成為線程安全函數(shù),該函數(shù)通過(guò)獲取每個(gè)容器的互斥量,來(lái)對(duì)兩個(gè)容器的訪問(wèn)進(jìn)行同步操作。

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

假設(shè)這個(gè)函數(shù)是由兩個(gè)不同的線程進(jìn)行調(diào)用的,***個(gè)線程中,一個(gè)元素從容器1中移除,添加到容器2中。第二個(gè)線程中,該元素又從容器2移除添加到容器1中。這種做法會(huì)導(dǎo)致發(fā)生死鎖(如果在獲取***個(gè)鎖后,線程上下文剛好從一個(gè)線程切換到另一個(gè)線程,導(dǎo)致發(fā)生死鎖)。

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

要解決這個(gè)問(wèn)題,可以使用std::lock來(lái)確保以避免發(fā)生死鎖的方式來(lái)獲取鎖。

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

#p#

條件變量C++11 還提供了另外一種同步原語(yǔ),就是條件變量,它能使一個(gè)或多個(gè)線程進(jìn)入阻塞狀態(tài),直到接到另一個(gè)線程的通知,或者發(fā)生超時(shí)或虛假喚醒時(shí),才退出阻塞.在頭文件<condition_variable> 里對(duì)條件變量有兩種實(shí)現(xiàn):

condition_variable:要求任何在等待該條件變量的線程必須先獲取std::unique_lock鎖。

Condition_variable_any:是一種更加通用的實(shí)現(xiàn),可以用于任意滿足鎖的基本條件的類型(該實(shí)現(xiàn)只要提供了lock()和 unlock()方法即可)。因?yàn)槭褂盟ㄙM(fèi)的代價(jià)比較高(從性能和操作系統(tǒng)資源的角度來(lái)講),所以只有在提供了必不可少的額外的靈活性的條件下才提倡使 用它。

下面來(lái)講講條件變量的工作原理: 至少有一個(gè)線程在等待某個(gè)條件變?yōu)閠rue。等待的線程必須先獲取unique_lock 鎖。該鎖被傳遞給wait()方法,wait()方法會(huì)釋放互斥量,并將線程掛起,直到條件變量接收到信號(hào)。收到信號(hào)后,線程會(huì)被喚醒,同時(shí)該鎖也會(huì)被重 新獲取。

至少有一個(gè)線程發(fā)送信號(hào)使某個(gè)條件變?yōu)閠rue。可以使用notify_one()來(lái)發(fā)送信號(hào),同時(shí)喚醒一個(gè)正在等待該條件收到信號(hào)的處于阻塞狀態(tài)的線程,或者用notify_all()來(lái)喚醒在等待該條件的所有線程。

在多處理器系統(tǒng)中,因?yàn)橐恍?fù)雜情況,要想完全預(yù)測(cè)到條件被喚醒并不容易,還會(huì)出現(xiàn)虛假喚醒的情況。就是說(shuō),在沒(méi)人給條件變量發(fā)送信號(hào)的情況下,線程也可能會(huì)被喚醒。所以線程被喚醒后,還需要檢測(cè)條件是否為true。因?yàn)榭赡軙?huì)多次發(fā)生虛假喚醒,所以需要進(jìn)行循環(huán)檢測(cè)。

下面代碼是一個(gè)使用條件變量來(lái)同步線程的例子:幾個(gè)工作線程運(yùn)行時(shí)可能會(huì)產(chǎn)生錯(cuò)誤并將錯(cuò)誤代碼放到隊(duì)列里。記錄線程會(huì)從隊(duì)列里取出錯(cuò)誤代碼并輸出它 們來(lái)處理這些錯(cuò)誤。發(fā)生錯(cuò)誤的時(shí)候,工作線程會(huì)給記錄線程發(fā)信號(hào)。記錄線程一直在等待條件變量接收信號(hào)。為了避免發(fā)生虛假喚醒,該等待過(guò)程在循環(huán)檢測(cè)條件 的布爾值。

 

  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; 
  12. bool                    g_done; 
  13. bool                    g_notified; 
  14.   
  15. void workerfunc(int id, std::mt19937& generator) 
  16.     // print a starting message 
  17.     { 
  18.         std::unique_lock<std::mutex> locker(g_lockprint); 
  19.         std::cout << "[worker " << id << "]\trunning..." << std::endl; 
  20.     } 
  21.   
  22.     // simulate work 
  23.     std::this_thread::sleep_for(std::chrono::seconds(1 + generator() % 5)); 
  24.   
  25.     // simulate error 
  26.     int errorcode = id*100+1; 
  27.     { 
  28.         std::unique_lock<std::mutex> locker(g_lockprint); 
  29.         std::cout  << "[worker " << id << "]\tan error occurred: " << errorcode << std::endl; 
  30.     } 
  31.   
  32.     // notify error to be logged 
  33.     { 
  34.         std::unique_lock<std::mutex> locker(g_lockqueue); 
  35.         g_codes.push(errorcode); 
  36.         g_notified = true
  37.         g_queuecheck.notify_one(); 
  38.     } 
  39.   
  40. void loggerfunc() 
  41.     // print a starting message 
  42.     { 
  43.         std::unique_lock<std::mutex> locker(g_lockprint); 
  44.         std::cout << "[logger]\trunning..." << std::endl; 
  45.     } 
  46.   
  47.     // loop until end is signaled 
  48.     while(!g_done) 
  49.     { 
  50.         std::unique_lock<std::mutex> locker(g_lockqueue); 
  51.   
  52.         while(!g_notified) // used to avoid spurious wakeups 
  53.         { 
  54.             g_queuecheck.wait(locker); 
  55.         } 
  56.   
  57.         // if there are error codes in the queue process them 
  58.         while(!g_codes.empty()) 
  59.         { 
  60.             std::unique_lock<std::mutex> locker(g_lockprint); 
  61.             std::cout << "[logger]\tprocessing error:  " << g_codes.front()  << std::endl; 
  62.             g_codes.pop(); 
  63.         } 
  64.   
  65.         g_notified = false
  66.     } 
  67.   
  68. int main() 
  69.     // initialize a random generator 
  70.     std::mt19937 generator((unsigned int)std::chrono::system_clock::now().time_since_epoch().count()); 
  71.   
  72.     // start the logger 
  73.     std::thread loggerthread(loggerfunc); 
  74.   
  75.     // start the working threads 
  76.     std::vector<std::thread> threads; 
  77.     for(int i = 0; i < 5; ++i) 
  78.     { 
  79.         threads.push_back(std::thread(workerfunc, i+1, std::ref(generator))); 
  80.     } 
  81.   
  82.     // work for the workers to finish 
  83.     for(auto& t : threads) 
  84.         t.join(); 
  85.   
  86.     // notify the logger to finish and wait for it 
  87.     g_done = true
  88.     loggerthread.join(); 
  89.   
  90.     return 0; 

運(yùn)行上述代碼,輸出結(jié)果如下(注意每次運(yùn)行,輸出結(jié)果都不一樣;因?yàn)槊總€(gè)工作線程運(yùn)行時(shí)都有一個(gè)隨機(jī)的休眠時(shí)間)。

  1. [logger]        running... 
  2. [worker 1]      running... 
  3. [worker 2]      running... 
  4. [worker 3]      running... 
  5. [worker 4]      running... 
  6. [worker 5]      running... 
  7. [worker 1]      an error occurred: 101 
  8. [worker 2]      an error occurred: 201 
  9. [logger]        processing error:  101 
  10. [logger]        processing error:  201 
  11. [worker 5]      an error occurred: 501 
  12. [logger]        processing error:  501 
  13. [worker 3]      an error occurred: 301 
  14. [worker 4]      an error occurred: 401 
  15. [logger]        processing error:  301 
  16. [logger]        processing error:  401 

上面看到的wait()方法有兩個(gè)重載:

  • ***個(gè)重載帶有鎖unique_lock;這個(gè)重載方法可以釋放鎖,阻塞線程,并把線程添加到正在等待這一條件變量的線程隊(duì)列里面。當(dāng)該條件變量收到信號(hào)或者發(fā)生虛假喚醒時(shí),線程就會(huì)被喚醒。它們其中任何一個(gè)發(fā)生時(shí),鎖都會(huì)被重新獲取,函數(shù)返回。
  • 第二個(gè)重載除了帶有鎖unique_lock外,還帶有循環(huán)判定直到返回false值;這個(gè)重載是用來(lái)避免發(fā)生虛假喚醒。它基本上等價(jià)于下面的語(yǔ)句:
  1. while(!predicate()) 
  2.    wait(lock); 

因此在上面的例子中,通過(guò)使用重載的wait()方法以及驗(yàn)證隊(duì)列狀態(tài)的判斷(空或不空),就可以避免使用布爾變量g_notified了。

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

除了這個(gè)重載的wait()方法,還有另外兩個(gè)類似的重載方法,也帶有避免虛假喚醒的判定。

  • Wait_for: 在條件變量收到信號(hào)或者指定的超時(shí)發(fā)生前,線程一直處于阻塞狀態(tài);
  • Wait_until:在條件變量收到信號(hào)或者指定的時(shí)刻到達(dá)之前,線程一直處于阻塞狀態(tài)。

這兩個(gè)函數(shù)的不帶有判定的重載返回cv_status狀態(tài),用來(lái)表明發(fā)生超時(shí)或者線程被喚醒是因?yàn)闂l件變量收到信號(hào)或者發(fā)生虛假喚醒。

該標(biāo)準(zhǔn)還提供了一個(gè)函數(shù)notify_all_at_thread_exit,它實(shí)現(xiàn)了一個(gè)機(jī)制,通知其他線程給定線程已經(jīng)運(yùn)行結(jié)束,并銷毀所有的 thread_local對(duì)象。該函數(shù)的引進(jìn)是因?yàn)樵谑褂昧藅hread_local后,采用除join()之外的其他機(jī)制來(lái)等待線程會(huì)導(dǎo)致不正確甚至致 命的行為發(fā)生。

因?yàn)閠hread_local的析構(gòu)函數(shù)會(huì)在等待中的線程恢復(fù)執(zhí)行和可能執(zhí)行結(jié)束的情況下被調(diào)用(可參考N3070和N2880得知更多信息)。

通常情況下,對(duì)這個(gè)函數(shù)的調(diào)用必須在線程生成之前。下面的例子描述了如何使用notify_all_at_thread_exit和condition_variable共同完成對(duì)兩個(gè)線程的同步操作:

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

如果工作線程在主線程執(zhí)行結(jié)束之前結(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é)束語(yǔ)

C++11標(biāo)準(zhǔn)可以讓C++開發(fā)者以一種標(biāo)準(zhǔn)的,獨(dú)立平臺(tái)的方式來(lái)編寫多線程。這篇文章大概講述了該標(biāo)準(zhǔn)所支持的線程和同步機(jī)制。頭文 件<thread>提供了thread類(和一些幫助函數(shù)),表明thread類是一個(gè)可執(zhí)行線程。頭文件<mutex>提供了 幾種互斥量的實(shí)現(xiàn)和對(duì)線程進(jìn)行同步訪問(wèn)的封裝類。頭文件<condition_variable>提供了條件變量的兩種實(shí)現(xiàn),這些實(shí)現(xiàn)使一個(gè) 或多個(gè)線程一直處于阻塞狀態(tài),直到接收到其他線程的通知,或發(fā)生超時(shí)或者有虛假喚醒發(fā)生時(shí)才會(huì)被喚醒。推薦讀者朋友可以閱讀其他資料來(lái)獲取更多的詳細(xì)信 息。

原文鏈接:http://blog.jobbole.com/44409/

責(zé)任編輯:陳四芳 來(lái)源: 伯樂(lè)在線
相關(guān)推薦

2013-05-30 00:49:36

C++11C++條件變量

2025-05-22 08:10:00

C++條件變量編程

2013-12-23 09:48:43

C++鎖定模式

2020-09-23 16:31:38

C++C++11啟動(dòng)線程

2024-02-21 23:43:11

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

2023-09-22 22:27:54

autoC++11

2024-05-29 13:21:21

2012-12-25 10:52:23

IBMdW

2020-06-01 21:07:33

C11C++11內(nèi)存

2011-08-19 09:41:56

C++

2024-06-24 08:10:00

C++互斥鎖

2013-09-25 14:20:46

2025-05-21 08:00:00

C++11關(guān)鍵字多線程

2021-06-11 10:53:40

Folly組件開發(fā)

2020-12-09 10:55:25

ArrayvectorLinux

2023-09-24 13:58:20

C++1auto

2016-11-23 16:08:24

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

2024-10-14 16:25:59

C#線程鎖代碼

2013-11-29 09:51:26

C++雙重檢查鎖定

2011-10-13 10:21:01

C++
點(diǎn)贊
收藏

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

主站蜘蛛池模板: 波多野结衣先锋影音 | 国产一区二区在线视频 | 黄色av网站免费看 | 日韩欧美高清dvd碟片 | 亚洲欧美综合 | 亚洲性视频在线 | 精品一区二区三区在线观看 | 欧美亚洲在线 | 四虎网站在线观看 | 国产一区二区三区四区 | 全部免费毛片在线播放网站 | 成人av影院| 久久久久久久综合 | 天堂一区| 在线观看中文字幕dvd播放 | 九九伦理电影 | 国产一级视频 | 午夜免费视频观看 | 国产精品久久久久无码av | 欧美美女爱爱 | 国产亚洲精品久久久久久豆腐 | 久久精品国产精品青草 | 成人性生交大片免费看r链接 | 中国一级大毛片 | 日韩成人免费 | 亚洲狠狠 | 亚洲精品字幕 | 国产美女福利在线观看 | 福利av在线 | 九九热国产精品视频 | 中文字幕在线观看 | 国产精品久久久久久久久久久久 | 国产区一区 | 日本不卡免费新一二三区 | 久久99精品久久久久久国产越南 | 国产一区二区三区四区五区加勒比 | 亚洲国产精品视频一区 | 在线国产精品一区 | 婷婷精品 | 国产91在线 | 亚洲 | 精品一区二区三 |