boost鎖機制中Semaphores的介紹

結構

  • What's A Semaphore?
  • Boost.Interprocess Semaphore Types And Headers
  • Anonymous semaphore example

What's A Semaphore?

  • 旗語是一種基于內部計數的進程間同步機制,它提供了兩種基本操作。
  • 等待:測試旗語數的值,如果小于或等于0,則等待,否則遞減旗語數。
  • Post:增加旗語數。增加信號量 如果有進程被阻塞,則喚醒其中一個進程。
  • 如果初始旗語數被初始化為1,則Wait操作相當于mutex鎖定,Post相當于mutex解鎖。這種類型的semaphore被稱為二進制semaphore。
  • 雖然旗語可以像mutexes一樣使用,但它們有一個獨特的特點:與mutexes不同,Post操作不需要由執行Wait操作的同一個線程/進程執行。

Boost.Interprocess Semaphore Types And Headers

  • Boost.Interprocess offers the following semaphore types:
  • #include <boost/interprocess/sync/interprocess_semaphore.hpp>
  • interprocess_semaphore: An anonymous semaphore that can be placed in shared memory or memory mapped files.
  • interprocess_semaphore。一個匿名的信號體,可以放在共享內存或內存映射文件中。
  • #include <boost/interprocess/sync/named_semaphore.hpp>
  • named_semaphore: A named semaphore.
  • named_semaphore。一個命名的旗語。

Anonymous semaphore example

  • We will implement an integer array in shared memory that will be used to transfer data from one process to another process. The first process will write some integers to the array and the process will block if the array is full.
  • The second process will copy the transmitted data to its own buffer, blocking if there is no new data in the buffer.
  • This is the shared integer array (doc_anonymous_semaphore_shared_data.hpp):
  • 我們將在共享內存中實現一個整數數組,用于從一個進程向另一個進程傳輸數據。第一個進程將向數組寫入一些整數,如果數組已滿,該進程將阻塞。
  • 第二個進程將把傳輸的數據復制到自己的緩沖區,如果緩沖區中沒有新的數據,則阻塞。
  • 這就是共享整數數組(doc_anonymous_semaphore_shared_data.hpp)。
#include <boost/interprocess/sync/interprocess_semaphore.hpp>struct shared_memory_buffer
{enum { NumItems = 10 };shared_memory_buffer(): mutex(1), nempty(NumItems), nstored(0){}//Semaphores to protect and synchronize accessboost::interprocess::interprocess_semaphoremutex, nempty, nstored;//Items to fillint items[NumItems];
};
  • 這是進程主進程。創建共享內存,將整數數組放置在那里,并逐個開始整數,如果數組滿了,則阻塞。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include "doc_anonymous_semaphore_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Remove shared memory on construction and destructionstruct 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);//Set sizeshm.truncate(sizeof(shared_memory_buffer));//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 memoryshared_memory_buffer * data = new (addr) shared_memory_buffer;const int NumMsg = 100;//Insert data in the arrayfor(int i = 0; i < NumMsg; ++i){data->nempty.wait();data->mutex.wait();data->items[i % shared_memory_buffer::NumItems] = i;data->mutex.post();data->nstored.post();}return 0;
}

The second process opens the shared memory and copies the received integers to it's own buffer:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
#include "doc_anonymous_semaphore_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Remove shared memory on destructionstruct shm_remove{~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(open_only                    //only create,"MySharedMemory"              //name,read_write  //read-write mode);//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 the shared structureshared_memory_buffer * data = static_cast<shared_memory_buffer*>(addr);const int NumMsg = 100;int extracted_data [NumMsg];//Extract the datafor(int i = 0; i < NumMsg; ++i){data->nstored.wait();data->mutex.wait();extracted_data[i] = data->items[i % shared_memory_buffer::NumItems];data->mutex.post();data->nempty.post();}return 0;
}
  • The same interprocess communication can be achieved with a condition variables and mutexes, but for several synchronization patterns, a semaphore is more efficient than a mutex/condition combination.
  • 同樣的進程間通信可以用條件變量和mutexes來實現,但對于幾種同步模式,semaphore比mutex/條件組合更有效率。

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

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

相關文章

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…

毛概 第二章新民主主義革命理論

1.新民主主義革命的總路線 2.

解決undefined reference to symbol ‘sem_close@@GLIBC_2.2.5‘問題

錯誤圖示 問題原因 編譯的時候&#xff0c;沒有引入庫文件 sem()位于pthread庫中&#xff0c;所以在編譯和鏈接時請確保使用-pthread標志&#xff0c;因此在編譯的時候需要導入pthread庫文件編譯的順序出現問題 解決辦法 在clion的CMakeLists.txt文件中添加這一行代碼set(CM…

Android 在onCreate()方法中獲取控件寬高值為0解決方案

大家很多時候需要在Activity或者Fragment的onCreate()方法中獲取聲明的空間的高度或者寬度&#xff0c;進行位置移動或者其他操作&#xff0c;但是當調用 view.getHeight() 或者 view.getWidth() 獲取的竟然為0。。。 其實很容易理解&#xff0c;在onCreate()的時候&#xff0…