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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2023/12/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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配合实现读写锁的全部內容,希望文章能夠幫你解決所遇到的問題。

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