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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用named_mutex和named_condition配合实现读写锁

發布時間:2023/12/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用named_mutex和named_condition配合实现读写锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼

  • 代碼的名稱是read_write_mutex.h
  • 初步優化
  • 如果涉及到進程掛掉了,造成進程堵塞,如何解決?還未涉及
#include <boost/interprocess/sync/named_condition.hpp> #include <boost/interprocess/sync/named_mutex.hpp>namespace bip = boost::interprocess; namespace chy{class shared_mutex{private:class state_data{public:state_data() :shared_count(0),exclusive(false),exclusive_waiting_blocked(false){}void assert_free() const{BOOST_ASSERT( ! exclusive );BOOST_ASSERT( shared_count==0);}void assert_locked() const{BOOST_ASSERT( exclusive);BOOST_ASSERT( shared_count==0);}void assert_lock_shared() const{BOOST_ASSERT( !exclusive);BOOST_ASSERT( shared_count>0 );}bool can_lock() const{return ! (shared_count || exclusive);}void exclusive_blocked (bool blocked){exclusive_waiting_blocked = blocked;}void lock(){exclusive = true;};void unlock(){exclusive = false;exclusive_waiting_blocked = false;}bool can_lock_shared() const{return !(exclusive || exclusive_waiting_blocked);}bool more_shared() const{return shared_count > 0;}uint8_t get_shared_count() const{return shared_count;}uint8_t lock_shared(){return ++shared_count;}uint8_t unlock_shared(){return --shared_count;}//TODO shared_count 的類型最好不要使用unsignedunsigned shared_count;//讀者數量bool exclusive;//表示已經加了寫鎖,進入互斥狀態bool exclusive_waiting_blocked;//表示即將加寫鎖,當其為true的時候,不可以加讀鎖,此時寫鎖競爭};state_data state;//TODO 將文件的名字和named_mutex和named_condition綁定在一起bip::named_mutex state_change{bip::open_or_create,"mutex"};bip::named_condition shared_cond{bip::open_or_create,"read"};bip::named_condition exclusive_cond{bip::open_or_create,"write"};void release_waiters(){exclusive_cond.notify_one();shared_cond.notify_all();}public:shared_mutex()=default;~shared_mutex()=default;void lock_shared(){boost::unique_lock<bip::named_mutex>lk(state_change);while (!state.can_lock_shared()){shared_cond.wait(lk);}state.lock_shared();}bool try_lock_shared(){boost::unique_lock<bip::named_mutex>lk(state_change);if (!state.can_lock_shared()){return false;}state.lock_shared();return true;}void unlock_shared(){boost::unique_lock<bip::named_mutex>lk(state_change);state.assert_lock_shared();state.unlock_shared();if (! state.more_shared()){state.exclusive_waiting_blocked = false;release_waiters();}}void lock(){boost::unique_lock<bip::named_mutex>lk(state_change);while (state.shared_count || state.exclusive){state.exclusive_waiting_blocked=true;exclusive_cond.wait(lk);}state.exclusive=true;}bool try_lock(){boost::unique_lock<bip::named_mutex>lk(state_change);if (state.shared_count || state.exclusive){return false;} else{state.exclusive=true;return true;}}void unlock(){boost::unique_lock<bip::named_mutex>lk(state_change);state.assert_locked();state.exclusive= false;state.exclusive_waiting_blocked= false;state.assert_free();release_waiters();}}; }//namespace chy

測試程序

#include <boost/thread/thread.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/ref.hpp>#include "read_write_mutex.h"#include <string>chy::shared_mutex global_mutex; int global_num = 10;//全局變量,寫者改變全局變量,讀者讀全局變量 namespace bip = boost::interprocess;//讀線程 void read_thread(std::string &name){boost::shared_lock<chy::shared_mutex> lock{global_mutex};printf("線程%s搶占了資源,global_num = %d\n",name.c_str(),global_num);boost::this_thread::sleep(boost::posix_time::seconds(1));printf("線程%s釋放了資源...\n",name.c_str()); }//寫線程 void write_thread(std::string &name){boost::lock_guard<chy::shared_mutex> lock{global_mutex};global_num++;//寫線程改變數據的數值printf("線程%s搶占了資源,global_num = %d\n",name.c_str(),global_num);boost::this_thread::sleep(boost::posix_time::seconds(1));printf("線程%s釋放了資源...\n",name.c_str()); }int main(){std::string read_thread_r1 = "read_thread_r1";std::string read_thread_r2 = "read_thread_r2";std::string read_thread_r3 = "read_thread_r3";std::string read_thread_r4 = "read_thread_r4";std::string read_thread_r5 = "read_thread_r5";std::string write_thread_w1 = "write_thread_w1";std::string write_thread_w2 = "write_thread_w2"; // std::string write_thread_w3 = "write_thread_w3"; // std::string write_thread_w4 = "write_thread_w4";boost::thread_group tg;tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r1)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r2)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r3)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r4)));tg.create_thread(boost::bind(read_thread,boost::ref(read_thread_r5)));tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w1)));tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w2))); // tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w3))); // tg.create_thread(boost::bind(write_thread,boost::ref(write_thread_w4)));tg.join_all();return 0; }

?

總結

以上是生活随笔為你收集整理的使用named_mutex和named_condition配合实现读写锁的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产综合精品在线 | 午夜资源 | 男男做的视频 | 欧美一区二区三区四区五区 | 久草视频福利在线 | 蜜臀在线一区二区三区 | 中文字幕一区二区人妻电影 | 亚洲第一页夜 | 性调教学院高h学校 | 国产盗摄av | av自拍偷拍| 伊人激情综合网 | 五月婷婷丁香 | 国产三级影院 | 欧美裸体xxx | 欧美片17c07.com | 午夜在线精品 | 欧美专区日韩专区 | 丰满尤物白嫩啪啪少妇 | 在线观看91 | 悠悠色影院 | 亚洲精品伦理 | 99久久精品日本一区二区免费 | 热精品| 久久久久久av无码免费网站下载 | 国产女同91疯狂高潮互磨 | 美女四肢被绑在床扒衣 | 青青草草 | 国产第一区第二区 | 喷水了…太爽了高h | jizz日本少妇 | 高清免费视频日本 | 色婷五月 | 毛片亚洲av无码精品国产午夜 | 欧美黄色图片 | av自拍网 | 性xxxfllreexxx少妇 | 在线天堂www在线国语对白 | 金瓶狂野欧美性猛交xxxx | 国产超碰人人模人人爽人人添 | 香蕉视频亚洲 | 色哟哟免费观看 | 欧美色图19p | 成人一级在线 | 国产精品亚洲二区 | 日本夜夜操 | yy6080久久 | 成人国产精品免费 | 在线观看网站黄 | 高清在线一区二区三区 | 伊人狼人综合 | 免费看黄色片视频 | 免费观看日韩毛片 | 久久久艹 | 久久激情综合 | 亚洲av成人无码久久精品 | 欧美一区二区在线看 | 视频在线一区 | 稀缺小u女呦精品呦 | 一级片视频网站 | 中文字幕 日韩 欧美 | 国产精品xxx在线观看www | 国产日产欧美一区二区三区 | a天堂中文字幕 | 国产欧美激情 | 国产999精品久久久久久 | 亚洲av成人精品毛片 | 成人精品av| 亚洲无码精品国产 | 亚洲一级特黄 | 牛人盗摄一区二区三区视频 | 熟妇人妻精品一区二区三区视频 | 黄瓜污视频 | 国产精品久久久久久妇女 | 天堂av一区| 97精品一区二区三区 | 亚洲欧洲免费 | 国产传媒视频 | 国产调教在线 | 国产主播专区 | 国产粉嫩一区二区三区 | 午夜天堂精品久久久久 | 日本大奶少妇 | 中文字幕第九页 | 亚洲再线| 九九在线视频 | 动漫大乳美女 | 亚洲av成人精品日韩在线播放 | 日韩一区二区免费播放 | 激情视频一区 | www.五月激情| 91网站免费入口 | 人妻丰满熟妇无码区免费 | 日韩无码专区 | 91国产免费视频 | 天天综合网永久 | 久久高清 | av天天网| 天天综合在线观看 |