Boost.Interprocess: Can I share the multi_index array between processes?
Hello, I would like to load multiindex container by one process lets say serverside_loader. And access that array by another clientside_analyser process? How to do that? Lets assume that I have a container: <code> struct particleID { int ID;// real ID for particle from Gadget2 file "ID" block unsigned int IDf;// postition in the file particleID(int id,const unsigned int idf):ID(id),IDf(idf){} bool operator<(const particleID& p)const { return ID<p.ID;} unsigned int getByGID()const {return (ID&0x0FFF);}; }; struct ID{}; struct IDf{}; struct IDg{}; typedef multi_index_container< particleID, indexed_by< ordered_unique< tag<IDf>, BOOST_MULTI_INDEX_MEMBER(particleID,unsigned int,IDf)>, ordered_non_unique< tag<ID>,BOOST_MULTI_INDEX_MEMBER(particleID,int,ID)>, ordered_non_unique< tag<IDg>,BOOST_MULTI_INDEX_CONST_MEM_FUN(particleID,unsigned int,getByGID)> >
particlesID_set; </code>
kind regards Arman. -- View this message in context: http://old.nabble.com/Boost.Interprocess%3A-Can-I-share-the-multi_index-arra... Sent from the Boost - Users mailing list archive at Nabble.com.
arm2arm <arm2arm <at> gmail.com> writes:
Hello, I would like to load multiindex container by one process lets say serverside_loader. And access that array by another clientside_analyser process? How to do that?
Hi Arman, Yes, this can be done. There's an example that shows you how to do it: http://www.boost.org/libs/multi_index/doc/examples.html#example12 Basically, you've got to take care of three things: * The element type (particleID in this case) must be placeable in shared memory, see http://tinyurl.com/ygau74j for info on the limitations imposed on such objects. particleID is a simple class and can be directly put in shared memory. * You must use a special Boost.Interprocess allocator for the multi-index container. Use the aforementioned example as a guide. * Access to the container must be properly synchronized, just as you'd do for instance in a multi-threaded program. Good luck with your project, come back if you have some difficulty. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hi, Thanks for your detailed explanations. I was paying with your nice example. Currently my structure is keeping only several floats, ints and unsigned ints. Following the Interprocess documentation, as you wrote, I can put them directly into shared memory: server_side: P.insert(particleID(0,0)); P.insert(particleID(-1,1)); P.insert(particleID(-2,2)); P.insert(particleID(-3,3)); try{ //Erase previous shared memory shared_memory_object::remove("shared_memory"); //Create a shared memory object. shared_memory_object shm (create_only, "shared_memory", read_write); //Set size size_t P_len=P.size()*sizeof(particleID); shm.truncate(P_len); //Map the whole shared memory in this process mapped_region region(shm, read_write); //Write all the memory to 1 std::memset(region.get_address(), 1, region.get_size()); print_out_by<ID>(P); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 100; for(;;)boost::thread::sleep(xt); } catch(interprocess_exception &ex){ shared_memory_object::remove("shared_memory"); std::cout << ex.what() << std::endl; return 1; } Now I am trying to access the memory from client: shared_memory_object shm (open_only, "shared_memory", read_only); //Map the whole shared memory in this process mapped_region region(shm, read_only); //Check that memory was initialized to 1 const particlesID_set *P = static_cast<particlesID_set*>(region.get_address()); //const particlesID_set::nth_index<1>::type L1_ID_index = (*P).get<1>();// This does not compile, Why? print_out_by<ID>((*P)); //This cannot access the memory... In Client I cannot get the index, it claims that cannot access to protected member. Maybe I am missing something?Could you please advice me? kind regards Arman. On Tue, Apr 6, 2010 at 8:59 PM, Joaquin M Lopez Munoz <joaquin@tid.es>wrote:
arm2arm <arm2arm <at> gmail.com> writes:
Hello, I would like to load multiindex container by one process lets say serverside_loader. And access that array by another clientside_analyser process? How to do that?
Hi Arman,
Yes, this can be done. There's an example that shows you how to do it:
http://www.boost.org/libs/multi_index/doc/examples.html#example12
Basically, you've got to take care of three things:
* The element type (particleID in this case) must be placeable in shared memory, see
for info on the limitations imposed on such objects. particleID is a simple class and can be directly put in shared memory. * You must use a special Boost.Interprocess allocator for the multi-index container. Use the aforementioned example as a guide. * Access to the container must be properly synchronized, just as you'd do for instance in a multi-threaded program.
Good luck with your project, come back if you have some difficulty.
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Arman Khalatyan escribió:
Hi, Thanks for your detailed explanations. I was paying with your nice example. Currently my structure is keeping only several floats, ints and unsigned ints. Following the Interprocess documentation, as you wrote, I can put them directly into shared memory:
[...]
Now I am trying to access the memory from client: shared_memory_object shm (open_only, "shared_memory", read_only);
//Map the whole shared memory in this process mapped_region region(shm, read_only);
//Check that memory was initialized to 1 const particlesID_set *P = static_cast<particlesID_set*>(region.get_address());
//const particlesID_set::nth_index<1>::type L1_ID_index = (*P).get<1>();// This does not compile, Why?
It is const particlesID_set::nth_index<1>::type& L1_ID_index = ... Note the added '&'; this is explained for instance at the final paragraph of http://www.boost.org/libs/multi_index/doc/tutorial/basics.html#multiple_sort Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Nice!!! Thank you for help!! The example 12 is fitting exactly to my requirements. The MultiIndex great library thanks for inventing that.:) Arman. On Wed, Apr 7, 2010 at 9:24 AM, <joaquin@tid.es> wrote:
Arman Khalatyan escribió:
Hi, Thanks for your detailed explanations. I was paying with your nice example. Currently my structure is keeping only several floats, ints and unsigned ints. Following the Interprocess documentation, as you wrote, I can put them directly into shared memory:
[...]
Now I am trying to access the memory from client: shared_memory_object shm (open_only, "shared_memory", read_only);
//Map the whole shared memory in this process mapped_region region(shm, read_only);
//Check that memory was initialized to 1 const particlesID_set *P = static_cast<particlesID_set*>(region.get_address());
//const particlesID_set::nth_index<1>::type L1_ID_index = (*P).get<1>();// This does not compile, Why?
It is
const particlesID_set::nth_index<1>::type& L1_ID_index = ...
Note the added '&'; this is explained for instance at the final paragraph of
http://www.boost.org/libs/multi_index/doc/tutorial/basics.html#multiple_sort
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ------------------------------------------ Dr Arman Khalatyan, Observatoire Astronomique de Marseille-Provence Laboratoire d'Astrophysique de Marseille -----------------------------------------
participants (4)
-
arm2arm
-
Arman Khalatyan
-
Joaquin M Lopez Munoz
-
joaquin@tid.es