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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

boost锁机制中Semaphores的介绍

發布時間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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/條件組合更有效率。

總結

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

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