响应式网站自助建站,google官网下载,企业展示型网站怎么建,网站优缺点结构
Whats A Semaphore?Boost.Interprocess Semaphore Types And HeadersAnonymous semaphore example
Whats A Semaphore?
旗语是一种基于内部计数的进程间同步机制#xff0c;它提供了两种基本操作。等待#xff1a;测试旗语数的值#xff0c;如果小于或等于0#x…结构
Whats A Semaphore?Boost.Interprocess Semaphore Types And HeadersAnonymous semaphore example
Whats 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.hppinterprocess_semaphore: An anonymous semaphore that can be placed in shared memory or memory mapped files.interprocess_semaphore。一个匿名的信号体可以放在共享内存或内存映射文件中。#include boost/interprocess/sync/named_semaphore.hppnamed_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.hppstruct 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.hppusing 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 its own buffer:
#include boost/interprocess/shared_memory_object.hpp
#include boost/interprocess/mapped_region.hpp
#include iostream
#include doc_anonymous_semaphore_shared_data.hppusing 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_castshared_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/条件组合更有效率。