http://blog.csdn.net/turner_gao/article/details/51914848
http://blog.csdn.net/great3779/article/details/7226388
http://blog.csdn.net/great3779/article/details/7240271
http://blog.chinaunix.net/uid-28595538-id-5068065.html
匿名互斥量举例
想象两个进程需要往一个共享内存中的循环缓冲区中写入轨迹。每个进程都需要排他地访问这个循环缓冲区,并将轨迹写入,然后继续运行。
为了保护循环缓冲区,我们可以保存一个进程共享的互斥量在这里面。每个进程将在写数据之前锁定该互斥量,并且在写入轨迹后,写入一个标志。
定义一个头文件doc_anonymous_mutex_shared_data.hpp:
#include <boost/interprocess/sync/interprocess_mutex.hpp>
struct shared_memory_log
{
enum { NumItems = 100 };
enum { LineSize = 100 };
shared_memory_log()
: current_line(0)
,end_a(false)
,end_b(false)
{}
boost::interprocess::interprocess_mutex mutex;
char items[NumItems][LineSize];
int current_line;
bool end_a;
bool end_b;
}
下面是主进程,创建共享内存,构造循环缓冲区和写轨迹:
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include “doc_anonymous_mutex_shared_data.hpp”
#include <iostream>
#include <cstdio>
using namespace boost::interprocess;
int main()
{
try{
struct shm_remove
{
shm_remove() { shared_memory_object::remove(“MySharedMemory”);}
~shm_remove() { shared_memory_object::remove(“MySharedMemory”);}
} remover;
shared_memory_object shm
( create_only
,”MySharedMemory”
,read_write );
shm.truncate(sizeof(shared_memory_log));
mapped_region region( shm,read_write);//类封装时作为成员变量
void * addr = region.get_address();
shared_memory_log * data = new (addr) shared_memory_log;
for( int i=0;i<shared_memory_log::NumItems;++i ){
scoped_lock<interprocess_mutex> lock(data->mutex);
std::sprintf( data->items[(data->current_line++) % shared_memory_log::NumItems],”%s_%d”,”process_a”,i);
if( i==(shared_memory_log::NumItems-1) )
data->end_a = true;
}
// 等待另外一个进程结束
while( 1 ){
scoped_lock<interprocess_mutex> lock(data->mutex);
if( data->end_b)
break;
}
}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 “doc_anonymous_mutex_shared_data.hpp”
#include <iostream>
#include <cstdio>
using namespace boost::interprocess;
int main(){
struct shm_remove{
~shm_remove(){ shared_memory_object::remove(“MySharedMemory”);}
} remover;
shared_memory_object shm( open_only,”MySharedMemory”,read_write );
mapped_region region( shm,read_write );//类封装时作为成员变量
void * addr = region.get_address();
shared_memory_log * data = static_cast<shared_memory_log*>(addr);
for( int i=0;i<100;++i ){
scoped_lock<interprocess_mutex> lock(data->mutex);
std::sprintf(data->items[(data->current_line++)%shared_memory_log::NumItems]
,”process_b_%d”,i);
if( i==(shared_memory_log::NumItems-1))
data->end_b = true;
}
while(1){
scoped_lock<interprocess_mutex> lock(data->mutex);
if( data->end_a)
break;
}
return 0;
}
正如我们看到的,互斥量在保护数据上非常实用,
类封装时,共享内存谁创建谁删除:
CShareMem::CShareMem(int iPlatForm/*=PLATE_SERVER*/)
:pShared_memory_data(NULL),iRegionSize(0)
,m_regionSer(NULL),m_regionCli(NULL),m_plateForm(iPlatForm)
{
switch (iPlatForm)
{
case PLATE_SERVER:
shared_memory_object::remove("SharedMemory");
break;
default:
break;
}
}
CShareMem::~CShareMem(void)
{
switch (m_plateForm)
{
case PLATE_SERVER:
shared_memory_object::remove("SharedMemory");
break;
default:
break;
}
}