溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++多線程中的鎖和條件變量使用教程

發布時間:2020-09-01 03:54:32 來源:腳本之家 閱讀:430 作者:Agang''''s blog 欄目:編程語言

在做多線程編程時,有兩個場景我們都會遇到:

  1. 多線程訪問共享資源,需要用到鎖;
  2. 多線程間的狀態同步,這個可用的機制很多,條件變量是廣泛使用的一種。

今天我用一個簡單的例子來給大家介紹下鎖和條件變量的使用。

代碼使用C++11

示例代碼

#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
std::mutex       g_mutex;   // 用到的全局鎖
std::condition_variable g_cond;   // 用到的條件變量
int g_i    = 0;
bool g_running = true;
void ThreadFunc(int n) {       // 線程執行函數
 for (int i = 0; i < n; ++i) {
  {
   std::lock_guard<std::mutex> lock(g_mutex);   // 加鎖,離開{}作用域后鎖釋放
   ++g_i;
   std::cout << "plus g_i by func thread " << std::this_thread::get_id() << std::endl;
  }
 }
 std::unique_lock<std::mutex> lock(g_mutex);    // 加鎖
 while (g_running) {
  std::cout << "wait for exit" << std::endl;
  g_cond.wait(lock);                // wait調用后,會先釋放鎖,之后進入等待狀態;當其它進程調用通知激活后,會再次加鎖
 }
 std::cout << "func thread exit" << std::endl;
}
int main() {
 int     n = 100;
 std::thread t1(ThreadFunc, n);    // 創建t1線程(func thread),t1會執行`ThreadFunc`中的指令
 for (int i = 0; i < n; ++i) {
  {
   std::lock_guard<std::mutex> lock(g_mutex);
   ++g_i;
   std::cout << "plus g_i by main thread " << std::this_thread::get_id() << std::endl;
  }
 }
 {
  std::lock_guard<std::mutex> lock(g_mutex);
  g_running = false;
  g_cond.notify_one();   // 通知其它線程
 }
 t1.join();     // 等待線程t1結束
 std::cout << "g_i = " << g_i << std::endl;
}

程序運行后,關鍵輸出如下:

plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by func thread 139921006847744
plus g_i by func thread 139921006847744
plus g_i by func thread 139921006847744
plus g_i by func thread 139921006847744
plus g_i by func thread 139921006847744
wait for exit               // func thread等待main thread發來的退出信號
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
plus g_i by main thread 139921025066816
func thread exit
g_i = 200     // 鎖機制保證了g_i的正確

可以看到:

std::this_thread::get_id()
g_i

加鎖方法介紹

加鎖相關的代碼為:

{
 std::lock_guard<std::mutex> lock(g_mutex);
 ......
}

要點為:

  • 首先,這在一個局部作用域內, std::lock_guard 在構造時,會調用 g_mutex->lock() 方法;
  • 局部作用域代碼結束后, std:;lock_guard 的析構函數會被調用,函數中會調用 g_mutex->unlock() 方法。

這樣就實現了加鎖和解鎖的過程,為什么不直接調用加鎖解鎖方法呢?

我想,這是因為如果加鎖和解鎖中間的代碼出現了問題,導致線程函數異常退出,那么這個鎖就一直無法得到釋放,其它線程處理的不好的話,就會造成死鎖了。

條件變量使用介紹

  • 當線程調用 g_cond.wait(lock) 前要先手動調用 lock->lock() ,這里是通過 std::unique_lock 的構造方法實現的;
  • 當線程調用 g_cond.wait(lock) 進入等待后,會調用 lock->unlock() 方法,所以這也是前面構造lock時使用了 std::unique_lock ;
  • 通知使用的 g_cond.notify_one() ,這個可以通知一個線程,另外還有 g_cond.notify_all() 用于通知所有線程;
  • 線程收到通知的代碼放在一個while循環中,這是為了防止APUE中提到的虛假通知。

結束語

以上所述是小編給大家介紹的C++多線程中的鎖和條件變量使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女