在做多線程編程時(shí),有兩個(gè)場(chǎng)景我們都會(huì)遇到:
- 多線程訪問(wèn)共享資源,需要用到鎖茫因;
- 多線程間的狀態(tài)同步,這個(gè)可用的機(jī)制很多杖剪,條件變量是廣泛使用的一種。
今天我用一個(gè)簡(jiǎn)單的例子來(lá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); // 加鎖盛嘿,離開(kāi){}作用域后鎖釋放
++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)用后,會(huì)先釋放鎖括袒,之后進(jìn)入等待狀態(tài)次兆;當(dāng)其它進(jìn)程調(diào)用通知激活后,會(huì)再次加鎖
}
std::cout << "func thread exit" << std::endl;
}
int main() {
int n = 100;
std::thread t1(ThreadFunc, n); // 創(chuàng)建t1線程(func thread)锹锰,t1會(huì)執(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ā)來(lái)的退出信號(hào)
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的正確
可以看到:
- 兩個(gè)線程有各自的線程id(thread id),通過(guò)
std::this_thread::get_id()
獲得恃慧; - 兩個(gè)線程交替執(zhí)行园蝠,需要有先后順序時(shí),就可以通過(guò)條件變量這種機(jī)制來(lái)做到痢士;
- 通過(guò)鎖機(jī)制(mutex)保證了
g_i
計(jì)算結(jié)果的正確
加鎖方法介紹
加鎖相關(guān)的代碼為:
{
std::lock_guard<std::mutex> lock(g_mutex);
......
}
要點(diǎn)為:
- 首先,這在一個(gè)局部作用域內(nèi),
std::lock_guard
在構(gòu)造時(shí)伦籍,會(huì)調(diào)用g_mutex->lock()
方法搀愧; - 局部作用域代碼結(jié)束后,
std:;lock_guard
的析構(gòu)函數(shù)會(huì)被調(diào)用城侧,函數(shù)中會(huì)調(diào)用g_mutex->unlock()
方法易遣。
這樣就實(shí)現(xiàn)了加鎖和解鎖的過(guò)程,為什么不直接調(diào)用加鎖解鎖方法呢嫌佑?
我想豆茫,這是因?yàn)槿绻渔i和解鎖中間的代碼出現(xiàn)了問(wèn)題,導(dǎo)致線程函數(shù)異常退出屋摇,那么這個(gè)鎖就一直無(wú)法得到釋放澜薄,其它線程處理的不好的話,就會(huì)造成死鎖了摊册。
條件變量使用介紹
- 當(dāng)線程調(diào)用
g_cond.wait(lock)
前要先手動(dòng)調(diào)用lock->lock()
肤京,這里是通過(guò)std::unique_lock
的構(gòu)造方法實(shí)現(xiàn)的; - 當(dāng)線程調(diào)用
g_cond.wait(lock)
進(jìn)入等待后,會(huì)調(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é)束語(yǔ)
上面是我對(duì)C++11中多線程加鎖和條件變量使用的基本認(rèn)識(shí)肯骇,有不當(dāng)?shù)牡胤娇耍€望指正。
參考
cppreference:https://en.cppreference.com/w/cpp/thread