日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

C++11中std::lock_guard的使用

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11中std::lock_guard的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

互斥類(lèi)的最重要成員函數(shù)是lock()和unlock()。在進(jìn)入臨界區(qū)時(shí),執(zhí)行l(wèi)ock()加鎖操作,如果這時(shí)已經(jīng)被其它線程鎖住,則當(dāng)前線程在此排隊(duì)等待。退出臨界區(qū)時(shí),執(zhí)行unlock()解鎖操作。更好的辦法是采用”資源分配時(shí)初始化”(RAII)方法來(lái)加鎖、解鎖,這避免了在臨界區(qū)中因?yàn)閽伋霎惓;騬eturn等操作導(dǎo)致沒(méi)有解鎖就退出的問(wèn)題。極大地簡(jiǎn)化了程序員編寫(xiě)mutex相關(guān)的異常處理代碼。C++11的標(biāo)準(zhǔn)庫(kù)中提供了std::lock_guard類(lèi)模板做mutex的RAII。

std::lock_guard類(lèi)的構(gòu)造函數(shù)禁用拷貝構(gòu)造,且禁用移動(dòng)構(gòu)造。std::lock_guard類(lèi)除了構(gòu)造函數(shù)和析構(gòu)函數(shù)外沒(méi)有其它成員函數(shù)

在std::lock_guard對(duì)象構(gòu)造時(shí),傳入的mutex對(duì)象(即它所管理的mutex對(duì)象)會(huì)被當(dāng)前線程鎖住。在lock_guard對(duì)象被析構(gòu)時(shí),它所管理的mutex對(duì)象會(huì)自動(dòng)解鎖,不需要程序員手動(dòng)調(diào)用lock和unlock對(duì)mutex進(jìn)行上鎖和解鎖操作。lock_guard對(duì)象并不負(fù)責(zé)管理mutex對(duì)象的生命周期,lock_guard對(duì)象只是簡(jiǎn)化了mutex對(duì)象的上鎖和解鎖操作,方便線程對(duì)互斥量上鎖,即在某個(gè)lock_guard對(duì)象的生命周期內(nèi),它所管理的鎖對(duì)象會(huì)一直保持上鎖狀態(tài);而lock_guard的生命周期結(jié)束之后,它所管理的鎖對(duì)象會(huì)被解鎖。程序員可以非常方便地使用lock_guard,而不用擔(dān)心異常安全問(wèn)題。

std::lock_guard在構(gòu)造時(shí)只被鎖定一次,并且在銷(xiāo)毀時(shí)解鎖。

關(guān)于std::mutex的基礎(chǔ)介紹可以參考: http://blog.csdn.net/fengbingchun/article/details/73521630?

The difference is that you can lock and unlock a std::unique_lock. std::lock_guard will be locked only once on construction and unlocked on destruction.

std::unique_lock has other features that allow it to e.g.: be constructed without locking the mutex immediately but to build the RAII wrapper. However, std::unique_lock might have a tad more overhead(較多開(kāi)銷(xiāo)).

std::lock_guard also provides a convenient RAII wrapper, but cannot lock multiple mutexes safely. It can be used when you need a wrapper for a limited scope, e.g.: a member function.

One of the differences between std::lock_guard and std::unique_lock is that the programmer is able to unlock std::unique_lock, but she/he is not able to unlock std::lock_guard.

下面是從其他文章中copy的測(cè)試代碼,詳細(xì)內(nèi)容介紹可以參考對(duì)應(yīng)的reference:

#include "lock_guard.hpp"
#include <iostream>
#include <thread>
#include <mutex>
#include <stdexcept>
#include <list>
#include <algorithm>namespace lock_guard_ {///
// reference: http://www.cplusplus.com/reference/mutex/lock_guard/
namespace {
std::mutex mtx;void print_even(int x) {if (x % 2 == 0) std::cout << x << " is even\n";else throw (std::logic_error("not even"));
}void print_thread_id(int id) {try {// using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:std::lock_guard<std::mutex> lck(mtx);print_even(id);} catch (std::logic_error&) {std::cout << "[exception caught]\n";}
}
}int test_lock_guard_1()
{std::thread threads[10];// spawn 10 threads:for (int i = 0; i<10; ++i)threads[i] = std::thread(print_thread_id, i + 1);for (auto& th : threads) th.join();return 0;
}///
// reference: http://www.cplusplus.com/reference/mutex/lock_guard/lock_guard/
namespace {
std::mutex mtx2;           // mutex for critical sectionvoid print_thread_id2(int id) {mtx2.lock();std::lock_guard<std::mutex> lck(mtx2, std::adopt_lock);std::cout << "thread #" << id << '\n';
}
}int test_lock_guard_2()
{std::thread threads[10];// spawn 10 threads:for (int i = 0; i<10; ++i)threads[i] = std::thread(print_thread_id2, i + 1);for (auto& th : threads) th.join();return 0;
}// reference: http://en.cppreference.com/w/cpp/thread/lock_guard
namespace {
int g_i = 0;
std::mutex g_i_mutex;  // protects g_ivoid safe_increment()
{std::lock_guard<std::mutex> lock(g_i_mutex);++g_i;std::cout << std::this_thread::get_id() << ": " << g_i << '\n';// g_i_mutex is automatically released when lock goes out of scope
}
}int test_lock_guard_3()
{std::cout << "main: " << g_i << '\n';std::thread t1(safe_increment);std::thread t2(safe_increment);t1.join();t2.join();std::cout << "main: " << g_i << '\n';return 0;
}//
// reference: http://www.bogotobogo.com/cplusplus/C11/7_C11_Thread_Sharing_Memory.php
namespace {
// a global variable
std::list<int> myList;// a global instance of std::mutex to protect global variable
std::mutex myMutex;void addToList(int max, int interval)
{// the access to this function is mutually exclusivestd::lock_guard<std::mutex> guard(myMutex);for (int i = 0; i < max; i++) {if ((i % interval) == 0) myList.push_back(i);}
}void printList()
{// the access to this function is mutually exclusivestd::lock_guard<std::mutex> guard(myMutex);for (auto itr = myList.begin(), end_itr = myList.end(); itr != end_itr; ++itr) {std::cout << *itr << ",";}
}
}
int test_lock_guard_4()
{int max = 100;std::thread t1(addToList, max, 1);std::thread t2(addToList, max, 10);std::thread t3(printList);t1.join();t2.join();t3.join();return 0;
}} // namespace lock_guard_

GitHub: https://github.com/fengbingchun/Messy_Test?

總結(jié)

以上是生活随笔為你收集整理的C++11中std::lock_guard的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲国产精品久久久久久6q | 国产欧美在线播放 | 扒开腿揉捏花蒂h | 一级片毛片| 日本人妻熟妇久久久久久 | 中文字幕亚洲精品在线观看 | 亚洲熟女少妇一区二区 | 色丁香婷婷综合久久 | 午夜在线视频免费观看 | 国产三级视频在线播放 | 台湾佬中文字幕 | 九九热在线精品 | 欧美日韩在线观看成人 | 国产精品成人免费精品自在线观看 | 欧美一区二区福利视频 | 五月婷婷六月激情 | 欧美精品一区三区 | 里番精品3d一二三区 | 亚洲一区二区精品在线观看 | www.chengren| 第四色影音先锋 | 影音先锋亚洲一区 | 日韩少妇一区二区三区 | 精品国产自在精品国产精小说 | 一级丰满大乳hd高清 | 在线一二区 | 99热这里只有精品9 日韩综合在线 | 日日操影院 | 亚洲视频在线一区 | 国产三级影院 | 国产a国产片国产 | 97人人爱| 热久久久| 日本午夜视频 | 色综合天天综合网天天狠天天 | 久久逼逼 | 清清草免费视频 | 久久国产视频播放 | 天天色综| 牛av| 熟妇人妻无乱码中文字幕真矢织江 | 床戏高潮做进去大尺度视频 | 亚洲av无码乱码国产精品fc2 | 四虎成人av | 亚洲熟女一区 | 欧美一级免费视频 | 日韩一区二区三区精品视频 | 五月99久久婷婷国产综合亚洲 | 美女的胸给男人玩视频 | 欧美日韩电影一区二区 | 91精品国产99 | 日韩在线一二三 | 日韩亚洲精品在线 | 精东av在线 | 国产精品第七页 | 日韩视频在线一区二区 | 黄色成年人网站 | 激情综合激情五月 | 欧美国产91| 欧美播放器| 日韩八区 | 人人干人人干 | 日韩一区二区精品视频 | 国内自拍视频在线播放 | 射在线 | 欧美久久久久 | 狠狠操综合 | 日本在线免费看 | www.日日操 | www.在线观看麻豆 | 色综合影视 | 国产精品久久久久久久久久久不卡 | 国产福利午夜 | 香蕉钻洞视频 | 久久久久一区二区三区四区 | 啪啪激情网 | 日本在线不卡一区二区三区 | 日本熟妇一区二区三区四区 | 桃色视屏 | 日韩欧美成人一区 | 一级视频黄色 | 国产一级理论片 | www夜插内射视频网站 | 无码人妻丰满熟妇啪啪欧美 | 高清国产视频 | 男女免费毛片 | 亚洲精品久久久久av无码 | 久久精选| 日本精品一区二区视频 | 欧美色精品在线 | 巨胸爆乳美女露双奶头挤奶 | 久久国产亚洲精品无码 | 欧美一卡二卡三卡 | 欧美性开放视频 | 中文字幕在线永久 | 欧美日韩国产中文字幕 | 国产日韩在线视频 | 亚洲欧美一区二区三区四区 | 国产精品成人自拍 |