//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)的更多相关文章

  1. boost信号量 boost::interprocess::interprocess_semaphore的用法

    使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...

  2. boost::interprocess(1)

    发送端:#include <iostream> #include <windows.h> #include <string> using namespace std ...

  3. boost::interprocess::managed_shared_memory(2)(std::deque)

    struct shareDataEx : shareData { int index; int total_size; }; typedef managed_shared_memory::segmen ...

  4. boost::interprocess::managed_shared_memory(2)(std::string)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include < ...

  5. boost::interprocess::shared_memory_object(1)(基本类型)

    #include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d ...

  6. Boost.Interprocess

    https://github.com/svebert/InterprocessMsg 好像消息队列

  7. Boost IPC Persistence Of Interprocess Mechanisms 例子

    下面这一段摘抄自 Boost 1_55_0 的文档,显然标注了 每一个的生命期. One of the biggest issues with interprocess communication m ...

  8. 用boost共享内存实现进程通信的例子

    发送端 #include "DBProc1.h" #include <string> #include <thread> #include <boos ...

  9. 详解boost库中的Message Queue .

    Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...

随机推荐

  1. Drupal启动阶段之四:系统变量

    Drupal的系统变量是指保存在后台数据库variable表中的一些参数设置,透过variable_get()和variable_set()存取: 先看一看_drupal_bootstrap_vari ...

  2. iOS 九宫格手势密码

    代码地址如下:http://www.demodashi.com/demo/11490.html 一.准备工作 需要准备什么环境 xcode,iOS8+ 本例子实现什么功能 主要实现手势密码设置,验证 ...

  3. 后台dubug有值且sql也打印出来执行了但是前台就是查不到数据

    记录在sturts2里面 摔得最深的一次 public String queryJoinAccount(){ //用来存储分页的数据 pageMap=new HashMap<String, Ob ...

  4. jQuery Growl 插件(消息提醒)

    jQuery Growl 插件(消息提醒) 允许您很容易地在一个覆盖层显示反馈消息.消息会在一段时间后自动消失,不需要单击"确定"按钮等.用户也可以通过移动鼠标或点击关闭按钮加快隐 ...

  5. 【ASP.NET MVC系列】详解View

    本篇文章内容属于ASP.NET MVC系列视图篇,主要讲解View,大致内容如下: 1.Views文件夹讲解 2.View种类 3.Razor语法 4.对视图的基本操作 一   Views文件夹 (一 ...

  6. mysql中实现分类汇总功能

    1.创建测试表: CREATE TABLE test_ROLLUP_1 ( StateCode ), DepCode ), SendMoney INT ); 2.插入测试语句: INSERT INTO ...

  7. 生成ID模板:年月日时分秒+6位自增码

    因为生成订单ID.商品ID 或者什么什么ID的,不想用自增,又怕反复,于是就用  年与日时分秒 + 6位自增码 (共计20位长度)来当作ID 注意:假设你的ID是Long型.就要注意,Long的最大长 ...

  8. nyoj 975 Distinct Count

    Distinct Count 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 给一个长度为 n 的数列 {an} ,找出有多少个长度为 m 的区间,使区间中不含有重复的数 ...

  9. PHP代码审计学习之命令执行漏洞挖掘及防御

    [1]可能存在命令执行漏洞的函数: 00x1:常用的命令执行函数:exec.system.shell_exec.passthru 00x2:常用的函数处理函数:call_user_func.call_ ...

  10. FormatUtil类型格式转换

    package cn.edu.hbcf.common.utils; import java.math.BigDecimal; import java.math.BigInteger; import j ...