C++多線程中的鎖和條件變量使用教程
在做多線程編程時(shí),有兩個(gè)場景我們都會遇到:
- 多線程訪問共享資源,需要用到鎖;
- 多線程間的狀態(tài)同步,這個(gè)可用的機(jī)制很多,條件變量是廣泛使用的一種。
今天我用一個(gè)簡單的例子來給大家介紹下鎖和條件變量的使用。
代碼使用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調(diào)用后,會先釋放鎖,之后進(jìn)入等待狀態(tài);當(dāng)其它進(jìn)程調(diào)用通知激活后,會再次加鎖
}
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結(jié)束
std::cout << "g_i = " << g_i << std::endl;
}
程序運(yùn)行后,關(guān)鍵輸出如下:
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 // 鎖機(jī)制保證了g_i的正確
可以看到:
std::this_thread::get_id() g_i
加鎖方法介紹
加鎖相關(guān)的代碼為:
{
std::lock_guard<std::mutex> lock(g_mutex);
......
}
要點(diǎn)為:
- 首先,這在一個(gè)局部作用域內(nèi), std::lock_guard 在構(gòu)造時(shí),會調(diào)用 g_mutex->lock() 方法;
- 局部作用域代碼結(jié)束后, std:;lock_guard 的析構(gòu)函數(shù)會被調(diào)用,函數(shù)中會調(diào)用 g_mutex->unlock() 方法。
這樣就實(shí)現(xiàn)了加鎖和解鎖的過程,為什么不直接調(diào)用加鎖解鎖方法呢?
我想,這是因?yàn)槿绻渔i和解鎖中間的代碼出現(xiàn)了問題,導(dǎo)致線程函數(shù)異常退出,那么這個(gè)鎖就一直無法得到釋放,其它線程處理的不好的話,就會造成死鎖了。
條件變量使用介紹
- 當(dāng)線程調(diào)用 g_cond.wait(lock) 前要先手動(dòng)調(diào)用 lock->lock() ,這里是通過 std::unique_lock 的構(gòu)造方法實(shí)現(xiàn)的;
- 當(dāng)線程調(diào)用 g_cond.wait(lock) 進(jìn)入等待后,會調(diào)用 lock->unlock() 方法,所以這也是前面構(gòu)造lock時(shí)使用了 std::unique_lock ;
- 通知使用的 g_cond.notify_one() ,這個(gè)可以通知一個(gè)線程,另外還有 g_cond.notify_all() 用于通知所有線程;
- 線程收到通知的代碼放在一個(gè)while循環(huán)中,這是為了防止APUE中提到的虛假通知。
結(jié)束語
以上所述是小編給大家介紹的C++多線程中的鎖和條件變量使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
欄 目:C語言
下一篇:C語言實(shí)現(xiàn)AT指令A(yù)SCII碼的拼接處理流程
本文標(biāo)題:C++多線程中的鎖和條件變量使用教程
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/652.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10如何判斷一個(gè)數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10如何判斷一個(gè)數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子


