在做多線程編程時,有兩個場景我們都會遇到:
今天我用一個簡單的例子來給大家介紹下鎖和條件變量的使用。
代碼使用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); ...... }
要點為:
這樣就實現了加鎖和解鎖的過程,為什么不直接調用加鎖解鎖方法呢?
我想,這是因為如果加鎖和解鎖中間的代碼出現了問題,導致線程函數異常退出,那么這個鎖就一直無法得到釋放,其它線程處理的不好的話,就會造成死鎖了。
條件變量使用介紹
結束語
以上所述是小編給大家介紹的C++多線程中的鎖和條件變量使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。