Boost logo

Boost Users :

Subject: Re: [Boost-users] How to Read the stored memory map in C++
From: manish4gupta (manish_at_[hidden])
Date: 2009-09-28 22:35:46


Thanks it works as suggested. I was in trouble from past 2 weeks but u helped
me. Once again thank you very much.

Roland Bock-2 wrote:
>
> manish4gupta wrote:
>> Previously i was doing in the same way but still getting the same error.
>> I
>> was removing he shared region in process2 only but pblm is still
>> there.Pls
>> can i get any working example doing the same.
>> Process1. storing map<string, int> nad process2 reading it.I will really
>> be
>> thankful to you.
>>
>>
>> Christoph Gysin-3 wrote:
>>> 2009/9/25 manish4gupta <manish_at_[hidden]>:
>>>> How can i solve my pblm? I tried many things but nothing seems to be
>>>> working.
>>> Calling shared_memory_object::remove("MySharedMemory") removes the
>>> whole shared memory where you saved your data. If it gets called
>>> before Process2 tries to read it, it will fail with mentioned
>>> interprocess_exception.
>>>
>>> Just don't remove it in Process1, remove it at the end of Process2.
>>>
>>> Chris
>>> --
>
> What Christoph wrote was correct:
>
> In the code you posted on th 18th, process1 removes the shared memory on
> exit (when the object called "remover" goes out of scope). Thus, when
> you run process1 it brings the shared memory into being, writes the map
> into it and then removes the whole stuff again.
>
> Thus, when you start process2 afterwards, the shared memory is long
> gone. Hence the exception.
>
> So in the code posted on the 18th, you just have to replace the remover
> by the following line:
>
> managed_shared_memory segment(create_only,"MySharedMemory", 65536);
>
> That works.
>
> BTW: In a "real world" application the remover would be a very useful
> helper class. If you do not know why, take a good book on C++, for
> instance "Effective C++" by Scott Meyers, and read until you do know :-)
>
>
> Regards,
>
> Roland
>
> PS: And please(!) remove the unnecessary stuff, before posting. The
> complex_data class for instance is totally irrelevant. Same goes for
> most of the typedefs. All this nonsense just adds to the noise which
> makes it harder to see the actual problem.
>
> PPS: Attached, please find the reduced code of process1.
>
>
>
>
> #include <boost/interprocess/managed_shared_memory.hpp>
> #include <boost/interprocess/allocators/allocator.hpp>
> #include <boost/interprocess/containers/map.hpp>
> #include <boost/interprocess/containers/string.hpp>
> #include <iostream>
>
> using namespace boost::interprocess;
>
> //Typedefs of allocators and containers
> typedef managed_shared_memory::segment_manager segment_manager_t;
> typedef allocator<void, segment_manager_t> void_allocator;
> typedef allocator<char, segment_manager_t> char_allocator;
> typedef basic_string<char, std::char_traits<char>, char_allocator>
> char_string;
>
> //Definition of the map holding a string as key and complex_data as mapped
> type
> typedef std::pair<const char_string, int> map_value_type;
> typedef allocator<map_value_type, segment_manager_t>
> map_value_type_allocator;
> typedef map< char_string, int, std::less<char_string>,
> map_value_type_allocator> complex_map_type;
>
> int main ()
> {
> shared_memory_object::remove("MySharedMemory");
>
> //Create shared memory
> managed_shared_memory segment(create_only,"MySharedMemory", 65536);
>
> //An allocator convertible to any allocator<T, segment_manager_t> type
> void_allocator alloc_inst (segment.get_segment_manager());
>
> //Construct the shared memory map and fill it
> complex_map_type *mymap =
> segment.construct<complex_map_type>("MyMap")(std::less<char_string>(),
> alloc_inst);
> char_string cs("test", alloc_inst);
>
> for(int i = 0; i < 100; ++i)
> {
> mymap->insert(std::pair<char_string, int>(cs , i));
> }
> return 0;
> }
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
View this message in context: http://www.nabble.com/How-to-Read-the-stored-memory-map-in-C%2B%2B-tp25506320p25656070.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net