能够在共享内存段中构造boost :: interprocess :: interprocess_mutex,但是当我尝试访问它时它为null

时间:2014-08-28 21:21:57

标签: c++ boost shared-memory

所以,我创建了一段共享内存。我在其中创建了一个boost :: interprocess :: vector,以及一个interprocess_mutex。

矢量工作正常。我可以得到它,它不是空的,我可以使用它。

当我尝试获取互斥锁时,它为空。

以下是设置:

using namespace boost::interprocess;

typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;

managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocator alloc_inst (segment.get_segment_manager());
//I can construct a vector just fine
segment.construct<MyVector>("MyVector")(alloc_inst);
//constructing mutex returns without error, but when I try to access it as below, it is null
segment.construct<interprocess_mutex>("MyMutex")();

稍后,当我尝试访问它时

  managed_shared_memory segment(open_only, "MySharedMemory");
  //The vector is there and not null
  MyVector *myvector = segment.find<MyVector>("MyVector").first;
  //The mutex is null when I try to access it.
  interprocess_mutex *mutex = segment.find<interprocess_mutex>("MyMutex").first;
  if (!mutex)
       //FAIL !!

我在这里做错了什么?

0 个答案:

没有答案