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库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
随机推荐
- .NET Remoting与Socket、Webservice和WCF的比较及优势 (转)
1:Socket VS Remoting 使用socket无疑是效率最高的.但是,在复杂的接口环境下,Socket的开发效率也是最低的.故在兼顾开发效率的情况下,可以使用Remoting来代替Sock ...
- mysql union和union all 的差别以及使用
Union由于要进行反复值扫描,所以效率低.假设合并没有刻意要删除反复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样.并且字段类型要"相容"(一致). ...
- Layui 弹出层组件——layer
layer是作为Layui[经典模块化前端框架]的一个弹层模块,由于其用户基数较大,所以把layer作为独立组件来维护. 基础参数: 基础参数主要指调用方法时用到的配置项,如:layer.open({ ...
- Javascript 中使用Json的四种途径
1.jQuery插件支持的转换方式: 复制代码代码如下: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 ...
- android studio - 导入工程报错[Plugin with id 'com.android.application' not found]
出错现象: 大概意思是找不到:com.android.application 插件,以上现象对于初学者来说会经常碰到,下面分析下产生的原因. 原因分析 首先来看看导入后的工程结构: 对于此工程结构,是 ...
- iptables 使用
原文链接 本文介绍25个常用的iptables用法.如果你对iptables还不甚了解,可以参考上一篇iptables详细教程:基础.架构.清空规则.追加规则.应用实例,看完这篇文章,你就能明白ipt ...
- makefile之变量
1 变量的定义 A variable is a name defined in a makefile to represent a string of text, called the variabl ...
- 如何查看VisualStudio的编译, 链接命令
VisualStudio默认是不显示编译命令的,如何查看呢. 对于链接器: 项目属性 -> 配置属性 -> 链接器 -> 常规 -> 显示进度 -> 设为 "/ ...
- getCanonicalPath getAbsolutePath区别
1.在winows环境下它们的区别是 getCanonicalPath是标准路径,没有特殊字符,getAbsolutePath是有特殊字符的 2.在AIX系统中它们的区别: 首先编译:javac ...
- regcomp/regexec/regfree--POSIX regex functions
语法 #include <sys/types.h> #include <regex.h> int regcomp(regex_t *preg, const char *rege ...