增强进程间Win32,x64

时间:2015-07-29 09:23:08

标签: c++ boost platform interprocess boost-interprocess

我想使用boost interprocess在不同平台之间进行通信。

我在Windows 7上使用vc12和boost 1.58。

下面的代码是一个非常简单的例子,应该可行。但它并不适用于不同平台之间的通信......

如果我在x64中创建msm并在win32中打开,则进程停在boost / int / sync / detail / common_algorithms.hpp中的函数try_based_lock的锁定

反过来说:win32 create,x64 open:进程在segment_manager_helper.hpp中的name_length崩溃,试图在索引中找到名称(segment_manager中的priv_generic_find)。

问题发生在msm.find(" Data")

#include <iostream>
#include <boost/interprocess/managed_shared_memory.hpp>

int main() {
  namespace bip = boost::interprocess;

  // open in WIN32, create in x64
#ifndef _WIN64
  bip::managed_shared_memory msm(bip::open_only, "TestIPC");
#else
  bip::shared_memory_object::remove("TestIPC");
  bip::managed_shared_memory msm(bip::create_only, "TestIPC", 4096);
  msm.construct<uint32_t>("Data")[1](10);
#endif

  // Get Data and print it
  auto data = msm.find<uint32_t>("Data");
  if (data.second == 1) {
    std::cout << *data.first << std::endl;
  }

  std::cin.ignore();

  return 0;
}

有没有人有这方面的经验?

1 个答案:

答案 0 :(得分:3)

使用托管共享内存类的32位和64位模式之间的

boost::interprocess通信似乎不受支持或者是错误。在较低的水平,但工作正常。以下是示例代码:

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>

int main(int argc, char *argv[])
{
    using namespace boost::interprocess;

#ifndef _WIN64
    shared_memory_object shm(open_only, "MySharedMemory", read_write);
    mapped_region region(shm, read_only);
    uint8_t *ptr = static_cast<uint8_t*>(region.get_address());
    std::cout << int32_t(*ptr) << std::endl;
#else
    shared_memory_object shm(create_only, "MySharedMemory", read_write);
    shm.truncate(4096);
    mapped_region region(shm, read_write);
    uint8_t *ptr = static_cast<uint8_t*>(region.get_address());
    *ptr = 5;
#endif

    return 0;
}