//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. cpu时间 / cpu利用率计算

    CPU时间即反映CPU全速工作时完成该进程所花费的时间 cpu时间计算CPU TIME = (# of CPU Clock Cycles) x Clock Period     // “#” 表示消耗 ...

  2. 【js中数组和字符串的相互转换】

    一.数组转字符串 //数组转字符串 var a, b; a = new Array(0,1,2,3,4); b = a.join(","); //得到字符串 二.字符串转数组 // ...

  3. nopi使用 设置列样式 宽高 设置分页符

    HSSFWorkbook book = new HSSFWorkbook(); ISheet sheet = book.CreateSheet("test_01"); sheet. ...

  4. sql关于group by之后把每一条记录的详情的某个字段值合并提取的方法

    在利用group by写了统计语句之后,还有一个查看每一个记录详情的需求, 首先想到的是根据group by的条件去拼接查询条件, 但是条件有点多,拼接起来不仅麻烦,还容易出错, 所以想到要在grou ...

  5. PHP+shell实现多线程的方法

    PHP+shell实现多线程的方法 这里介绍怎样借助shell脚本实现多线程. 先写个简单的php代码.这里为了让脚本运行时间更长.方便看效果,sleep一下.呵呵.先看下test.php的代码:ls ...

  6. C语言第十一回合:预处理命令的集中营

    C语言第十一回合:预处理命令的集中营   [学习目标]   1.         宏定义 2.         文件包括"处理 3.         条件编译 预处理命令:能够改进程序设计的 ...

  7. 171. Anagrams【medium】

    Given an array of strings, return all groups of strings that are anagrams. Notice All inputs will be ...

  8. CCNA2.0笔记_路由分类

    直连路由:当在路由器上配置了接口的IP地址,并且接口状态为up的时候,路由表中就出现直连路由项 静态路由:静态路由是由管理员手工配置的,是单向的. 默认路由:当路由器在路由表中找不到目标网络的路由条目 ...

  9. 通过ip查看主机名和端口占用情况

      1. 知道对方ip查看对方的计算机名 方法:开始->运行->cmd->net view 对方ip 或者 开始->运行->cmd->nbtstat -a 对方ip ...

  10. 跟着百度学PHP[17]-复习PDO的几个关键词

    主要就是复习一下几个关键词.发现太久没有写又忘了.惭愧. final self static const