C++ Boost::Interprocess Shared memory with struct

Posted

技术标签:

【中文标题】C++ Boost::Interprocess Shared memory with struct【英文标题】: 【发布时间】:2014-09-30 14:11:03 【问题描述】:

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

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

#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)

【问题讨论】:

如何向我们展示错误所在的代码,并将所有这些格式化以使其可读? 【参考方案1】:

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

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 Shared memory with struct的主要内容,如果未能解决你的问题,请参考以下文章

boost::interprocess_mutex 与进程本地 boost::mutex

为啥我不能使用 Boost.Interprocess 设置从一个进程通过另一个进程创建的一些内存?

在类对象段错误中使用 boost::interprocess,为啥?

boost::interprocess::string 转换为 char*

boost::interprocess::interprocess_condition::wait 在等待时不会原子地解锁互斥锁

boost::interprocess::message_queue 权限被拒绝