boost::interprocess(2)


//doc_anonymous_mutex_shared_data.hpp
#include <boost/interprocess/sync/interprocess_mutex.hpp>

struct shared_memory_log
{
enum { NumItems = 100 };
enum { LineSize = 100 };

shared_memory_log()
: current_line(0)
, 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(500);
//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 1;
}
return 0;
}

接收端:


#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 = 0;
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(300);

}

}

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 = 0; i < 100; ++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-1))
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 0;
}

有点BUG,在调调

时间: 2024-10-13 02:39:21

boost::interprocess(2)的相关文章

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

struct shareDataEx : shareData { int index; int total_size; }; typedef managed_shared_memory::segment_manager segment_manager_t; //段管理器 typedef allocator<shareDataEx, segment_manager_t> mem_allocator; //定义基于shareDataEx类型的分配器 typedef deque<shareDa

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

#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/interprocess/containers/string.hpp> using namespace std; int main() { //boost::inter

boost::interprocess(1)

发送端:#include <iostream> #include <windows.h> #include <string> using namespace std; #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/mapped_region.hpp> #include <thread> using namespa

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

#include <iostream> #include <boost/interprocess/managed_shared_memory.hpp> struct pos2d { int x; int y; }; using namespace std; int main() { //boost::interprocess::shared_memory_object类是按照单个字节的方式读写共享内存,用起来不方便 boost::interprocess::shared_memor

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

使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. 1 boost::interprocess::interprocess_semaphore m_semaphore(0); 然后就是pv操作了,v操作就只有一个post(),post()一次,信号量加1.p操作有三个,看函数名字都很明显知道是什么意思, wait(),try_wait() ,timed_wait(const boost::posix_time::ptime&abs_time)

boost的named_mutex的一些坑

最近遇到一个问题,程序在a用户下运行后,然后注销windows,登陆b用户,发现程序奔溃,抓了下堆栈,发现了boost的named_mutex一些细节,记录下 #include <boost/interprocess/sync/named_mutex.hpp> #include <boost/interprocess/creation_tags.hpp> boost::interprocess::named_mutex mutex(boost::interprocess::open

基于boost实现的共享内存版HashMap

#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/unordered_map.hpp> #include <boost/functional/hash.hpp> #include <functional> int main (int argc,

Boost 15 进程间通信

1. 介绍 Boost.Interprocess库简化了使用通用的进程间通信和同步机制.并且提供这些机制的部件: * 共享内存 * 内存映射文件 * 信号量,互斥量,条件变量和可升级的互斥量类型,该类型可以放入共享内存和内存映射文件中 * 命名版本的同步对象 * 文件锁 * 相对指针 * 消息队列 Boost.Interprocess还提供了更高级的进程间机制,用于动态分配共享内存或者内存映射文件.: * 在共享内存或者文件映射中动态创建匿名或者命名对象. * 与共享内存或者文件映射兼容的类似S

Boost:shared_memory_object --- 共享内存

什么是共享内存 共享内存是最快速的进程间通信机制.操作系统在几个进程的地址空间上映射一段内存,然后这几个进程可以在不需要调用操作系统函数的情况下在那段内存上进行读/写操作.但是,在进程读写共享内存时,我们需要一些同步机制. 考虑一下服务端进程使用网络机制在同一台机器上发送一个HTML文件至客户端将会发生什么: 服务端必须读取这个文件至内存,然后将其传至网络函数,这些网络函数拷贝那段内存至操作系统的内部内存. 客户端使用那些网络函数从操作系统的内部内存拷贝数据至它自己的内存. 如上所示,这里存在两