我希望能在shared_memory_object
已经存在的情况下,为读取同步。这是我使用bool变量进行同步的代码。
boost::interprocess::shared_memory_object my_shared_mat;
bool mat_ready = true;
while (mat_ready)
{
try {
my_shared_mat = boost::interprocess::shared_memory_object(
boost::interprocess::open_only, // only open
"shared_mat", // name
boost::interprocess::read_only); // read-only mode
mat_ready = false;
}
catch (boost::interprocess::interprocess_exception &ex) {
std::cout << ex.what() << std::endl;
mat_ready = true;
}
}
boost::interprocess::mapped_region region(my_shared_mat, boost::interprocess::read_only);
如果共享内存存在,我就没有任何问题,数据会在进程之间共享。但是如果共享内存不存在,在调用mapped_region时程序就会崩溃。
mapped_region
函数。我建议你展示真实的代码。 - Jonathan Potter