问题
I am trying to share a structure across processes using interprocess in Boost.
I've defined the mapped file to use null mutex because I was having problems with it locking and I don't mind doing the synchronisation myself.
What I am having problems with though is finding of objects.
I have the following declaration:
typedef boost::interprocess::basic_managed_mapped_file
< char,
boost::interprocess::rbtree_best_fit<boost::interprocess::null_mutex_family,boost::interprocess::offset_ptr<void>>,
boost::interprocess::flat_map_index>
my_mapped_file;
In process A, I do:
m_managedMappedFile.reset(new my_mapped_file(bip::open_or_create, filename, filesize));
auto hdr = m_managedMappedFile->find_or_construct<Foo>(bip::unique_instance)();
auto x = m_managedMappedFile->find<Foo>(bip::unique_instance);
Which works as I would expect, i.e. it finds the object. Now, in process B:
m_managedMappedFile.reset(new my_mapped_file(bip::open_only, filename));
auto ret = m_managedMappedFile->find<Foo>(bip::unique_instance);
For some reason the find method returns null in process B. I realise I must be doing something daft, but can't figure it out.
Can anyone help?
回答1:
You should not have to bypass the locking mechanism of the default bip::managed_mapped_file
indexes.
See if you can run the following with success:
#include <iostream>
#include <boost/interprocess/managed_mapped_file.hpp>
namespace bip = boost::interprocess;
struct X {
int i;
};
int main()
{
{
bip::managed_mapped_file f(bip::open_or_create, "/tmp/mmf.bin", 1ul << 20);
if (!f.find<X>(bip::unique_instance).first) {
auto xp = f.find_or_construct<X>(bip::unique_instance)();
assert(xp);
xp->i = 42;
}
}
{
bip::managed_mapped_file f(bip::open_only, "/tmp/mmf.bin");
auto xp = f.find<X>(bip::unique_instance).first;
if (xp)
std::cout << "value: " << xp->i++ << "\n";
}
}
This should print 42
on the first run (or after the file has been recreated), and increasing numbers on each subsequent run.
I'm going to have a look at the implementation behind the unique_instance_t*
overloads of the segment managers, but I suspect they might not work because the mutex policy was nulled. This is just a hunch though, at the moment.
I'd focus on finding out why you can't get Interprocess managed_mapped_file
working in the default configuration, on your platform & installation.
来源:https://stackoverflow.com/questions/28413207/boost-interprocess-managed-mapped-file-find-failing