C++ Boost::Interprocess 共享内存与结构

标签 c++ linux boost-interprocess

我搜索在两个进程之间共享一个结构。但我没有成功。你能帮忙理解吗?

这是我的第一个过程的代码:

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include "semaphore_shared_data.hpp"

using namespace boost::interprocess;


int main(){

shared_memory_object shm(open_or_create, "shared_memory", read_write);
shm.truncate(sizeof(shared_memory_buffer));
mapped_region region(shm,read_write);

void *addr = region.get_address();
shared_memory_buffer *data = new (addr) shared_memory_buffer;
while(true){

   data->writer.wait();
   std::cout << "Process 1 read: " << data->Variable.Type << ":" <<data->Variable.Value << std::endl;

  data->Variable.Type = "ACK";
  data->Variable.Value = 1;
  sleep(1);
  data->reader.post();
}
return 0;
}

这是我的第二个过程的代码:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <iostream>
#include <math.h>
#include <string>
#include "semaphore_shared_data.hpp"

using namespace boost::interprocess;


int main ()
{

 shared_memory_object shm(open_only, "shared_memory",read_write);
 mapped_region region(shm,read_write);
 void *addr = region.get_address();

 shared_memory_buffer *data = static_cast<shared_memory_buffer*>(addr);
 while(true)
 {

   data->reader.wait();
   std::cout << "Process 2 read: " << data->Variable.Type << ":" <<    data->Variable.Value << std::endl;
   data->Variable.Type = "ACK";
   data->Variable.Value = 2;
   data->writer.post();
 }
 return 0;
}

这是我的 shared_memory_buffer (semaphore_shared_data.hpp) 代码

#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
using namespace boost::interprocess;

typedef allocator<char, managed_shared_memory::segment_manager>  CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;

struct shared_Struct
{
  shared_Struct():Type("ACK"), Value(0){}
  shared_string Type;
  float Value;

};

struct shared_memory_buffer 
{

   shared_memory_buffer(): writer(1), reader(0), Variable(){}
   interprocess_semaphore writer, reader;

   shared_Struct Variable;

};

我有这个错误:

semaphore_shared_data.hpp: In constructor ‘boost::container::basic_string<CharT, Traits, Allocator>::basic_string(const CharT*, const allocator_type&) [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::container::basic_string<CharT, Traits, Allocator>::allocator_type = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’:

semaphore_shared_data.hpp:16:38: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’ shared_Struct():Type("ACK"), Value(0){}

最佳答案

我认为我的错误是由于这个声明:

typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator; 
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;

gcc 表示:

shared_Struct():Type("ACK"), Value(0){}

关于C++ Boost::Interprocess 共享内存与结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26122973/

相关文章:

PHP - 由于 Linux 权限,无法创建新文件夹和新文件

linux - 如何使用awk查找基于两列的唯一值

synchronization - 名为 mutex 的 boost 进程间在崩溃后仍保持获取状态

c++ - C++ 中的错误分配异常

c++ - 在字符串字段中查找字符串的索引

linux - 删除所有 node_modules 子文件夹

c++ - 提升.MultiIndex : Are there way to share object between two processes?

c++ - try_lock() +unlock() 是检查 boost::interprocess::file_lock 是否被锁定的有效方法吗?

c++ - 使用 C++ 在 MySQL 中为 MFC Dialog Base App 插入一行

c++ - 通过引用将函数对象传递给标准算法