C++多線程中的鎖和條件變量使用教程
在做多線程編程時,有兩個場景我們都會遇到:
- 多線程訪問共享資源,需要用到鎖;
- 多線程間的狀態(tài)同步,這個可用的機制很多,條件變量是廣泛使用的一種。
今天我用一個簡單的例子來給大家介紹下鎖和條件變量的使用。
代碼使用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) { // 線程執(zhí)行函數(shù)
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調用后,會先釋放鎖,之后進入等待狀態(tài);當其它進程調用通知激活后,會再次加鎖
}
std::cout << "func thread exit" << std::endl;
}
int main() {
int n = 100;
std::thread t1(ThreadFunc, n); // 創(chuàng)建t1線程(func thread),t1會執(zhí)行`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發(fā)來的退出信號 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 的析構函數(shù)會被調用,函數(shù)中會調用 g_mutex->unlock() 方法。
這樣就實現(xiàn)了加鎖和解鎖的過程,為什么不直接調用加鎖解鎖方法呢?
我想,這是因為如果加鎖和解鎖中間的代碼出現(xiàn)了問題,導致線程函數(shù)異常退出,那么這個鎖就一直無法得到釋放,其它線程處理的不好的話,就會造成死鎖了。
條件變量使用介紹
- 當線程調用 g_cond.wait(lock) 前要先手動調用 lock->lock() ,這里是通過 std::unique_lock 的構造方法實現(xiàn)的;
- 當線程調用 g_cond.wait(lock) 進入等待后,會調用 lock->unlock() 方法,所以這也是前面構造lock時使用了 std::unique_lock ;
- 通知使用的 g_cond.notify_one() ,這個可以通知一個線程,另外還有 g_cond.notify_all() 用于通知所有線程;
- 線程收到通知的代碼放在一個while循環(huán)中,這是為了防止APUE中提到的虛假通知。
結束語
以上所述是小編給大家介紹的C++多線程中的鎖和條件變量使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
C++的cout.tellp()和cout.seekp()語法介紹
無論是使用 cout 輸出普通數(shù)據(jù),用 cout.put() 輸出指定字符,還是用 cout.write() 輸出指定字符串,數(shù)據(jù)都會先放到輸出流緩沖區(qū),待緩沖區(qū)刷新,數(shù)據(jù)才會輸出到指定位置,本文給大家介紹一下C++的cout.tellp()和cout.seekp()語法,需要的朋友可以參考下2023-09-09
C++實現(xiàn)LeetCode(110.平衡二叉樹)
這篇文章主要介紹了C++實現(xiàn)LeetCode(110.平衡二叉樹),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07
詳解C語言中的ttyname()函數(shù)和isatty()函數(shù)的用法
這篇文章主要介紹了C語言中的ttyname()函數(shù)和isatty()函數(shù)的用法,是C語言入門學習中的基礎知識,需要的朋友可以參考下2015-09-09

