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库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
随机推荐
- 一个基于node express4.0和mongodb的活动报名
代码放在code.csdn.net上了,详细https://code.csdn.net/qazwsx2345/node_activity/tree/master git clone git@code. ...
- SSH限制ip登陆
linux限制IP访问ssh 在/etc/hosts.allow输入 (其中192.168.10.88是你要允许登陆ssh的ip,或者是一个网段192.168.10.0/24) sshd: ...
- wx小程序的学习
传送门: # 微信小程序案例大全 https://www.cnblogs.com/icyhoo/p/6282574.html # 微信小程序开发工具 https://mp.weixin.qq.com/ ...
- myeclipse的user library不自动部署到WEB-INF/lib目录下的解决办法
右键进入项目属性 Properties 点进去 左边 MyEclipse -> Deployment Assembly 右边 Add -> JavaBuild Path Entries - ...
- object-c全局变量
跟c++一定,在.m里Obj*obj=NULL,在.h里extern Obj*obj 即可.
- thinkphp验证码出不来
import("ORG.Util.Image"); //图像操作类库 ob_end_clean(); $type = isset($_GET['type'])?$_GET['typ ...
- java使用lambda表达式对List集合进行操作(JDK1.8)
1. 使用lambda表达式对List集合进行操作(JDK1.) List<TreeDto> collect = list.stream().filter(item -> item. ...
- 使用Lua的扩展库LuaSocket用例
目录结构 LuaSocket 是 Lua 的网络模块库,它可以很方便地提供 TCP.UDP.DNS.FTP.HTTP.SMTP.MIME 等多种网络协议的访问操作. 它由两部分组成:一部分是用 C 写 ...
- WEEX快速入门
WEEX快速入门 WEEX 是阿里推送的一款基于Node.js,轻量级的移动端跨平台动态性技术解决方案,用于构建原生的速度的跨平台APP. 1. 搭建WEEX环境 1.1 首先下载安装Node.js, ...
- font-sqirrel
html5之前,只要稍微特殊点的字体,都必须做成图片,以免客户端无法显示.而对于正文或者需要后台调用大量文字的地方,则无能为力.但是,html5推出了 @font-face 支持自定义字体之后,这个问 ...