對于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;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446849.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446849.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446849.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++數據類型

C 數據類型 |–基本數據類型: 整型 short短整型 2 int基本整型4 long長整型 8 浮點型 float單精度型 4 double雙精度型 8 long double長雙精度型 16 字符型 char 1 邏輯型 bool 1 空類型 void |–構造類型 數組類型 構造體類型 struct 共用體類型 union 枚舉類型 enum 類類型…

Android Gradle 多渠道打包、動態配置AppName

目錄一、簡介二、Gradle多渠道打包1、普通做法2、Gradle多渠道打包一、簡介 因為國內Android應用分發市場的現狀&#xff0c;我們在發布APP時&#xff0c;一般需要生成多個渠道包&#xff0c;上傳到不同的應用市場。這些渠道包需要包含不同的渠道信息&#xff0c;在APP和后臺交…

boost鎖機制中Semaphores的介紹

結構 Whats A Semaphore?Boost.Interprocess Semaphore Types And HeadersAnonymous semaphore example Whats A Semaphore? 旗語是一種基于內部計數的進程間同步機制&#xff0c;它提供了兩種基本操作。等待&#xff1a;測試旗語數的值&#xff0c;如果小于或等于0&#x…

Android Gradle 批量修改生成的apk文件名

目錄一、簡介二、代碼實現1、 Gradle 3.0以下版本2、Gradle 3.0以上版本一、簡介 平時開發都知道&#xff0c;我們要上線的時候需要在Android studio打包apk文件&#xff0c;可是默認的打包名是app-release.apk或者app-debug.apk這樣的名字&#xff0c;太沒有辨識度了。 下面…

C++boost Class named_condition翻譯

Class named_condition boost::interprocess::named_condition 簡介 // In header: <boost/interprocess/sync/named_condition.hpp>class named_condition { public:// construct/copy/destructnamed_condition(create_only_t, const char *, const permissions &…

Android Studio 代理設置以及代理完全清除

目錄一、代理設置二、代理完全清除一、代理設置 首先我們來看下怎樣設置代理&#xff0c;Mac下打開【Preferences…】&#xff0c;然后搜索"HTTP"&#xff0c;選擇【HTTP Proxy】&#xff0c;按圖中設置配置好后&#xff0c;點擊【Apply】&#xff0c;然后在點擊【O…

安卓布局位置,dp與px的區別

手機6寸—指對角線 布局位置 橫軸—x軸 縱軸—y軸 一個像素點 dp與Px dp:設備無關像素,與像素密度相關,像素距離 dpi:像素密度,每英寸包含的像素數 px:屏幕上一個物理像素點 ldpi低密度 1dp0.75px mdpi中密度 1dp1px hdpi高密度 1dp1.5px xhdpi超高密度 1dp2px xxhdpi超…

Android Studio 快捷鍵大全(Mac系統)

目錄一、Mac上的按鍵符號二、快捷鍵查找/查看相關控制操作相關代碼重構相關一、Mac上的按鍵符號 符號說明?option / alt?shift?control?command?esc 二、快捷鍵 查找/查看相關 快捷鍵說明雙擊 shift搜索任意內容command F / command R當前文件查找/替換&#xff0c;使…

ubuntu下clion軟件連接boost庫文件

整體配置 cmake_minimum_required(VERSION 3.17) project(mutex_learn)set(CMAKE_CXX_STANDARD 14) #boost庫所在的根目錄set(BOOST_ROOT "/usr/local/include/boost") #添加頭文件搜索路徑 include_directories(/usr/local/include) #添加庫文件搜索路徑 link_dir…

Android程序結構

Project方式 .gradle文件夾:編譯相關生成 .idea文件夾:idea生成 app文件夾----應用程序的源代碼和資源 build----編譯后的文件存放的位置,最終apk文件存放的位置 libs:存放.jar和.so文件 src:AndroidTest與test存放測試相關的內容 main中Java文件夾存放Java源碼,res文件…

通過Github創建Android庫供其他項目依賴引用

目錄一、簡介二、實現第一步&#xff1a;將自己的項目托管到Github上。第二步&#xff1a;點擊releases。第三步&#xff1a;創建release。第四步&#xff1a;填寫版本號、名稱、描述信息。第五步&#xff1a;點擊【Publish release】。第六步&#xff1a;復制項目路徑。第七步…

使用boost模板函數實現讀寫鎖

介紹 shared_mutex即讀寫鎖&#xff0c;不同與我們常用的獨占式鎖mutex&#xff0c;shared_mutex是共享與獨占共存的鎖&#xff0c;實現了讀寫鎖的機制&#xff0c;即多個讀線程一個寫線程&#xff0c;通常用于對于一個共享區域的讀操作比較頻繁&#xff0c;而寫操作比較少的情…

安卓內邊距padding與外邊距magrin

內邊距padding與外邊距margin 內邊距只有容器才有,即里面要有視圖 具體示例

Android Studio發布項目到jcenter

目錄一、創建Bintray賬戶及Maven倉庫二、上傳項目到jcenter1、配置Android項目2、Gradle命令上傳3、在項目中引用4、Add to JCenter三、Demo示例一、創建Bintray賬戶及Maven倉庫 1、打開Bintray首頁&#xff0c;點擊 For an Open Source Account &#xff0c;快速注冊或者用第…

C++讀取文件,將文件內容讀到string字符串里面

使用stringstream和ifstream實現 代碼 std::ifstream f{file_name, std::ios::binary};std::stringstream ss;ss << f.rdbuf();auto data ss.str();

Android MotionEvent中getX()、getRawX()和getTop()的區別

為了展示getX()、getRawX()和getTop()方法的區別&#xff0c;我們寫了一個簡單的測試Demo&#xff0c;我們寫了一個自定義控件&#xff08;繼承ImageView&#xff09;。 package com.demo.ui;import android.content.Context; import android.support.annotation.Nullable; im…

C++常量

常量 1.字面常量與符號常量 字面常量:從字面形式可以識別的常量 eg:1.2;‘A’ 整型常量:八進制(以0開頭),十六進制(以0x或0X開頭) 浮點型常量: 小數形式(整數和小數可以省略其中之一------為0時) eg:.123(0.123) 123.(123.0) 指數形式 0.23e1(0.2310^1) 0.23E-2(0.2310^-2)…

Synchronization 進程鎖

Boost.Interprocess允許多個進程同時使用共享內存。因為共享內存從定義上來說是進程間共享的&#xff0c;所以Boost.Interprocess需要支持某種同步。想到同步&#xff0c;我們會想到C11標準庫中的類或Boost.Thread。但是這些類只能用來同步同一進程內的線程&#xff0c;它們不支…

Android 獲取屏幕寬度和高度的幾種方法

方法一&#xff1a; public static void getScreenSize1(Context context){WindowManager windowManager (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display defaultDisplay windowManager.getDefaultDisplay();Point point new Point();defaultD…