boost::interprocess(2)
//doc_anonymous_mutex_shared_data.hpp
#include <boost/interprocess/sync/interprocess_mutex.hpp> struct shared_memory_log
{
enum { NumItems = };
enum { LineSize = }; shared_memory_log()
: current_line()
, end_a(false)
, end_b(false)
{} //Mutex to protect access to the queue
boost::interprocess::interprocess_mutex mutex; //Items to fill
char items[NumItems][LineSize];
int num; 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>
#include <windows.h>
#include <thread>
using namespace boost::interprocess;
mapped_region* p_reg; void funs(shared_memory_log * data)
{
while(true)
{
{
//Write some logs
//Lock the mutex
scoped_lock<interprocess_mutex> lock(data->mutex);
/*std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems]
,"%s_%d", "process_a", i);*/
data->num++;
//if(i == (shared_memory_log::NumItems-1))
// data->end_a = true;
//Mutex is released here }
Sleep();
//Wait until the other process ends
/*while(1){
scoped_lock<interprocess_mutex> lock(data->mutex);
if(data->end_b)
break;
}*/
}
} int main ()
{
try{
//Remove shared memory on construction and destruction
//struct 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
(open_or_create //only create
,"MySharedMemory" //name
,read_write //read-write mode
); //Set size
shm.truncate(sizeof(shared_memory_log)); //Map the whole shared memory in this process
p_reg = new mapped_region
(shm //What to map
,read_write //Map it as read-write
); //Get the address of the mapped region
void * addr = p_reg->get_address(); //Construct the shared structure in memory
shared_memory_log * data = new (addr) shared_memory_log; std::thread th(funs, data);
th.detach();
getchar();
shared_memory_object::remove("MySharedMemory");
}
catch(interprocess_exception &ex){
std::cout << ex.what() << std::endl;
shared_memory_object::remove("MySharedMemory");
getchar();
return ;
}
return ;
}
接收端:
#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>
#include <thread>
#include <windows.h>
using namespace boost::interprocess;
mapped_region* p_reg;
int g_num = ;
void fung(shared_memory_log * data)
{
while (true)
{
{
scoped_lock<interprocess_mutex> lock(data->mutex);
std::cout << data->num << "---------" << data->num - g_num << std::endl;
g_num = data->num;
}
Sleep(); } } int main ()
{
//如何保证会删除
//Remove shared memory on destruction
//struct shm_remove
//{
// ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
//} remover; //Open the shared memory object.
shared_memory_object shm
(open_only //only open
,"MySharedMemory" //name
,read_write //read-write mode
); //Map the whole shared memory in this process
p_reg = new mapped_region
(shm //What to map
,read_write //Map it as read-write
); //Get the address of the mapped region
void * addr = p_reg->get_address(); //Construct the shared structure in memory
shared_memory_log * data = static_cast<shared_memory_log*>(addr);
#if 0
//Write some logs
for(int i = ; i < ; ++i){
//Lock the mutex
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-))
data->end_b = true;
//Mutex is released here
}
#endif
//读log
#if 1
std::thread th(fung, data);
th.detach(); #endif
getchar();
shared_memory_object::remove("MySharedMemory");
//Wait until the other process ends
//while(1){
// scoped_lock<interprocess_mutex> lock(data->mutex);
// if(data->end_a)
// break;
//}
return ;
}
有点BUG,在调调
boost::interprocess(2)的更多相关文章
- boost信号量 boost::interprocess::interprocess_semaphore的用法
使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...
- boost::interprocess(1)
发送端:#include <iostream> #include <windows.h> #include <string> using namespace std ...
- boost::interprocess::managed_shared_memory(2)(std::deque)
struct shareDataEx : shareData { int index; int total_size; }; typedef managed_shared_memory::segmen ...
- boost::interprocess::managed_shared_memory(2)(std::string)
#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...
- boost::interprocess::shared_memory_object(1)(基本类型)
#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...
- Boost.Interprocess
https://github.com/svebert/InterprocessMsg 好像消息队列
- Boost IPC Persistence Of Interprocess Mechanisms 例子
下面这一段摘抄自 Boost 1_55_0 的文档,显然标注了 每一个的生命期. One of the biggest issues with interprocess communication m ...
- 用boost共享内存实现进程通信的例子
发送端 #include "DBProc1.h" #include <string> #include <thread> #include <boos ...
- 详解boost库中的Message Queue .
Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
随机推荐
- 利用Redis撤销JSON Web Token产生的令牌
利用Redis撤销JSON Web Token产生的令牌 作者:chszs.版权全部.未经允许,不得转载.博主主页:http://blog.csdn.net/chszs 早先的博文讨论了在Angula ...
- 使用Nexus创建私服
原文链接:http://www.cnblogs.com/helong/articles/2254446.html 注意:nexus2.6.0版本以后不支持jdk6了,必须jdk1.7或以上. 参考:h ...
- Latex中设置字体颜色
在用Latex时,想要使用不同颜色来突出某些关键点,有以下三种方案: 1.组合red.green和blue的值合成我们想要的颜色 \usepackage{color} \textcolor[rgb]{ ...
- JBoss类隔离
http://tiger888.iteye.com/blog/572875这几天,项目组在部署JBOSS时遇到不少问题,都是由于JBOSS的类装载问题引起,特发表一篇BLOG详细说一下JBOSS的类隔 ...
- jquery.validate.js 验证框架详解
项目中遇到这个js框架 jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求.该插件捆绑了一套有用的验证方法,包括 ...
- 两种方法一句代码隐藏Activity的标题栏
把Activity的标题栏隐藏有两种方法.一种是在在Activity里面设置javacode.还有一种是在项目的清单文件AndroidManifest.xml中设置模版样式. 一.在Activity中 ...
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse 原文:http ...
- Powershell - 获取OS版本信息和catpion信息
Environment 获取 OSversion: $OSVersion = [System.Environment]::OSVersion.Version WMI获取Caption: $OSCap ...
- vivado error [Labtools 27-2149]
使用vivado2014.3时遇到了这种错误 ERROR: [Labtools 27-2149] File E:/project/V7_PCIE/prj/PhasedProject/M_PcieGen ...
- PHPCMS V9数据库表结构分析
PHPCMS V9可以轻松承载百万级的访问数据,最大的功臣就是PHPCMS良好的数据库结构,在数据库的设计方面,一定是下足了功夫. 一般网站的信息量离这个级别相差甚远,但是了解学习一下PHPCMS ...