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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

对于boost锁机制结论性的介绍

發布時間:2023/12/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对于boost锁机制结论性的介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Conditions

  • What's A Condition Variable?
  • Boost.Interprocess Condition Types And Headers
  • Anonymous condition example

What's A Condition Variable?

  • 在前面的例子中,一個mutex被用來鎖定,但我們不能用它來有效地等待,直到滿足繼續的條件。一個條件變量可以做兩件事。
  • wait: 線程被阻塞,直到其他線程通知它可以繼續,因為導致等待的條件已經消失。
  • 通知:線程通知其他線程可以繼續。線程向一個被阻塞的線程或所有被阻塞的線程發送一個信號,告訴它們引起它們等待的條件已經消失。
  • 條件變量中的等待總是與一個mutex相關聯。在等待條件變量之前,必須先鎖定該mutex。當在條件變量上等待時,線程解鎖mutex并原子性地等待。
  • 當線程從等待函數中返回時(例如因為信號或超時),mutex對象再次被鎖定。

Boost.Interprocess Condition Types And Headers

  • Boost.Interprocess offers the following condition types:
  • #include <boost/interprocess/sync/interprocess_condition.hpp>
  • interprocess_condition。一個匿名的條件變量,可以放在共享內存或內存映射文件中,與 boost::interprocess::interprocess_mutex 一起使用。
  • #include <boost/interprocess/sync/interprocess_condition_any.hpp>
  • interprocess_condition_any.一個匿名的條件變量,可以放在共享內存或內存映射文件中,用于任何鎖類型。一個匿名的條件變量,可以放在共享內存或內存映射文件中,用于任何鎖類型。
  • #include <boost/interprocess/sync/named_condition.hpp>
  • named_condition.一個命名的條件變量,與 named_mutex 一起使用。與named_mutex一起使用的命名條件變量。
  • #include <boost/interprocess/sync/named_condition_any.hpp>
  • named_condition: 一個命名的條件變量,可用于任何類型的鎖。
  • 命名條件與匿名條件類似,但它們與命名的mutexes結合使用。好幾次,我們不想用同步數據來存儲同步對象。我們希望使用相同的數據改變同步方法(從進程間到進程內,或者沒有任何同步)。將進程共享的匿名同步與同步數據一起存儲將禁止這樣做。我們希望通過網絡或其他通信方式發送同步數據。發送過程共享的同步對象就沒有任何意義了。

Anonymous condition example

  • 想象一下,一個進程,將一個跟蹤寫到一個簡單的共享內存緩沖區,另一個進程逐一打印。第一個進程寫入跟蹤并等待另一個進程打印數據。為了達到這個目的,我們可以使用兩個條件變量:第一個條件變量用來阻止發送者,直到第二個進程打印信息,第二個條件變量用來阻止接收者,直到緩沖區有痕跡要打印。
  • 共享內存跟蹤緩沖區(doc_anonymous_condition_shared_data.hpp)
#include <boost/interprocess/sync/interprocess_mutex.hpp> #include <boost/interprocess/sync/interprocess_condition.hpp>struct trace_queue {enum { LineSize = 100 };trace_queue(): message_in(false){}//Mutex to protect access to the queueboost::interprocess::interprocess_mutex mutex;//Condition to wait when the queue is emptyboost::interprocess::interprocess_condition cond_empty;//Condition to wait when the queue is fullboost::interprocess::interprocess_condition cond_full;//Items to fillchar items[LineSize];//Is there any messagebool message_in; };
  • 這是該進程的主進程。創建共享內存,將緩沖區放置在那里,并開始逐一寫入消息,直到寫下 "最后一條消息",表示沒有更多的消息要打印。
#include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <iostream> #include <cstdio> #include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main () {//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0; }
  • 第二個過程打開共享內存,打印每一條消息,直到收到 "最后一條消息 "消息。
#include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <iostream> #include <cstdio> #include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main () {//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0; }
  • 通過條件變量,一個進程如果不能繼續工作就可以阻塞,當滿足繼續工作的條件時,另一個進程可以喚醒它。
#include <boost/interprocess/shared_memory_object.hpp> #include <boost/interprocess/mapped_region.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <iostream> #include <cstring> #include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main () {//Create a shared memory object.shared_memory_object shm(open_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Obtain a pointer to the shared structuretrace_queue * data = static_cast<trace_queue*>(addr);//Print messages until the other process marks the endbool end_loop = false;do{scoped_lock<interprocess_mutex> lock(data->mutex);if(!data->message_in){data->cond_empty.wait(lock);}if(std::strcmp(data->items, "last message") == 0){end_loop = true;}else{//Print the messagestd::cout << data->items << std::endl;//Notify the other process that the buffer is emptydata->message_in = false;data->cond_full.notify_one();}}while(!end_loop);}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0; }

?

總結

以上是生活随笔為你收集整理的对于boost锁机制结论性的介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产乱国产 | 色婷婷久久综合中文久久蜜桃av | 免费国产一区二区 | 不卡一区在线 | 亚洲激情免费 | 免费在线播放黄色片 | 成人欧美日韩 | 在线一区二区三区四区 | 一区二区三区在线观看av | 欧美爱爱免费视频 | 亚洲高清精品视频 | 中国老熟妇自拍hd发布 | 51ⅴ精品国产91久久久久久 | 国产成人无码av在线播放dvd | 91高清网站 | 中文字幕日韩国产 | 亚洲久久色 | 蜜乳av懂色av粉嫩av | 调教女m荡骚贱淫故事 | 午夜视频网址 | 性爱免费在线视频 | 草久在线 | 粉嫩aⅴ一区二区三区 | 97av视频 | 91精品国产综合久久国产大片 | 黄色免费网| 亚洲天堂国产精品 | 亚洲国产成人综合 | 欧美精品1区 | 一区二区三区四区五区视频 | 香蕉手机网 | 国产精品久久视频 | 亚洲天堂一二三 | av在线资源站 | 日日爽日日操 | 久久精品这里只有精品 | 午夜粉色视频 | 亚洲精选久久 | 久久国产精品久久国产精品 | 亚洲精品无 | 青青草免费观看 | 丝袜老师办公室里做好紧好爽 | 青青毛片 | 九热在线 | 成人免费无码大片a毛片 | 爱情岛论坛亚洲品质自拍视频 | 国产亚洲一区在线 | 久久老女人 | www.青青草 | 三区在线 | 国产精品亚州 | 波多野结衣乳巨码无在线观看 | 91视频黄 | 中文字幕首页 | 久久一级黄色片 | 免费中文字幕在线观看 | 四虎精品一区二区三区 | 国产精品国产三级国产普通话对白 | 日日骑| 色婷婷av一区二区三区软件 | 亚洲男女啪啪 | 99色在线视频 | 99视频观看 | 国产大片黄 | 精品少妇一区二区三区密爱 | 欧美人与动物xxx | 日韩一区av在线 | 欧美成人aaaaⅴ片在线看 | 午夜av免费观看 | 五月婷婷六月合 | jizz一区二区三区 | 九九热精| 麻豆黄色网址 | 97毛片| 色图自拍| 亚洲精品一区二区 | 亚州av片 | 久久国产乱子伦免费精品 | 椎名空在线 | 亚洲50p | 三级特黄视频 | 一级片一区二区三区 | 在线中文字幕视频 | 夜夜嗨av一区二区三区免费区 | 91久久精品国产91性色69 | 男人天堂新地址 | 果冻传媒18禁免费视频 | 在线亚洲综合 | 日韩成人精品 | 77777av | 男女无套免费视频网站动漫 | 香蕉视频官网在线观看 | jlzzjlzz亚洲日本少妇 | 打开免费观看视频在线 | 亚洲国产精品免费在线观看 | 欧美美女一区二区三区 | 国产欧美日韩久久 | 香蕉视频一区二区 | 日本黄网在线观看 |