C++ 在fork()期间使用Boost库共享STL对象

C++ 在fork()期间使用Boost库共享STL对象,c++,boost,ipc,shared-memory,boost-interprocess,C++,Boost,Ipc,Shared Memory,Boost Interprocess,我尝试共享STL对象(例如无序地图) 我读到,我们可以使用Boost库来轻松地使用进程之间的共享内存。 我使用了给定的代码 编写器代码如下所示: #include <bits/stdc++.h> #include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/allocators/allocator.hpp> #include <boost/uno

我尝试共享STL对象(例如无序地图)

我读到,我们可以使用Boost库来轻松地使用进程之间的共享内存。 我使用了给定的代码

编写器代码如下所示:

#include <bits/stdc++.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#include <boost/unordered_map.hpp>     //boost::unordered_map
#include <functional>                  //std::equal_to
#include <boost/functional/hash.hpp>   //boost::hash

int main ()
{
   using namespace boost::interprocess;
   //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 shared memory
   managed_shared_memory segment(create_only, "MySharedMemory", 65536);

   //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
   //so the allocator must allocate that pair.
   typedef int    KeyType;
   typedef float  MappedType;
   typedef std::pair<const int, float> ValueType;

   //Typedef the allocator
   typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

   //Alias an unordered_map of ints that uses the previous STL-like allocator.
   typedef boost::unordered_map
      < KeyType               , MappedType
      , boost::hash<KeyType>  ,std::equal_to<KeyType>
      , ShmemAllocator>
   MyHashMap;

   //Construct a shared memory hash map.
   //Note that the first parameter is the initial bucket count and
   //after that, the hash function, the equality function and the allocator
   MyHashMap *myhashmap = segment.construct<MyHashMap>("MyHashMap")  //object name
      ( 3, boost::hash<int>(), std::equal_to<int>()                  //
      , segment.get_allocator<ValueType>());                         //allocator instance

   //Insert data in the hash map
   for(int i = 0; i < 100; ++i){
      myhashmap->insert(ValueType(i, (float)i*23.5));
   }

   MyHashMap :: iterator iter;
   iter = myhashmap->begin();

   std::cout << "Hoooo.... i am in the server!!\n";

   for (; iter != myhashmap->end(); iter++) {
      std::cout << iter->first << " " << iter->second << "\n";
   }


   return 0;
}
#include <bits/stdc++.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>

#include <boost/unordered_map.hpp>     //boost::unordered_map
#include <functional>                  //std::equal_to
#include <boost/functional/hash.hpp>   //boost::hash

int main ()
{
   using namespace boost::interprocess;
   //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 shared memory
   managed_shared_memory segment(open_only, "MySharedMemory");

   //Note that unordered_map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
   //so the allocator must allocate that pair.
   typedef int    KeyType;
   typedef float  MappedType;
   typedef std::pair<const int, float> ValueType;

   //Typedef the allocator
   typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

   //Alias an unordered_map of ints that uses the previous STL-like allocator.
   typedef boost::unordered_map
      < KeyType               , MappedType
      , boost::hash<KeyType>  ,std::equal_to<KeyType>
      , ShmemAllocator>
   MyHashMap;

   //Construct a shared memory hash map.
   //Note that the first parameter is the initial bucket count and
   //after that, the hash function, the equality function and the allocator
   MyHashMap *myhashmap = segment.find<MyHashMap>("MyHashMap").first;  //object name
      // ( 3, boost::hash<int>(), std::equal_to<int>()                  //
      // , segment.get_allocator<ValueType>());                         //allocator instance


   MyHashMap :: iterator iter;
   std::cout << "in client e\n";
   iter = myhashmap->begin();

   std::cout << "Hoooo.... i am in the client!!\n";

   for (; iter != myhashmap->end(); iter++) {
      std::cout << iter->first << " " << iter->second << "\n";
   }

   segment.destroy<MyHashMap>("MyHashMap");


   return 0;
}
我不理解我所犯的错误,因为我对Boost库完全陌生,并且在我的一个项目中遇到了它


我还想知道我是否可以在使用
fork()
创建的某个进程中包含reader部分(因为我之前也尝试过同样的方法,但得到了相同的错误)。

你解决了吗?@aprospero抱歉,但我无法用那种方式解决它。取而代之的是,我使用了另一种方法(有效),即只有父进程将承载所有数据结构,子进程将通过
命名管道
发送
信号
s和最小数据以启用数据更新。希望有帮助!你设法解决了吗?@aprospero对不起,我不能那样解决。取而代之的是,我使用了另一种方法(有效),即只有父进程将承载所有数据结构,子进程将通过
命名管道
发送
信号
s和最小数据以启用数据更新。希望有帮助!
terminate called after throwing an instance of 'boost::interprocess::interprocess_exception'
  what():  No such file or directory
Aborted (core dumped)