[interprocess] example for boost::interprocess::map where the key is something in shared memory
does anyone have a simple example of a map in shared memory, where the
key is also in shared memory & not a simple "int"?
I am trying to use a key of type
boost::interprocess::basic_string
does anyone have a simple example of a map in shared memory, where the
key is also in shared memory & not a simple "int"?
I am trying to use a key of type
boost::interprocess::basic_string
Jason Sachs wrote:
does anyone have a simple example of a map in shared memory, where the key is also in shared memory & not a simple "int"?
This example will go in the "quick guide for the impatient" section ASAP.
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2006-2008. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/interprocess for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include
Ion Gaztañaga
//An allocator convertible to any allocator
type void_allocator alloc_inst (segment.get_segment_manager()); //Construct the shared memory map and fill it complex_map_type *mymap = segment.construct
//(object name), (first ctor parameter, second ctor parameter) ("MyMap")(std::less (), alloc_inst); for(int i = 0; i < 100; ++i){ //Both key(string) and value(complex_data) need an allocator in their constructors char_string key_object(alloc_inst); complex_data mapped_object(i, "default_name", alloc_inst); map_value_type value(key_object, mapped_object); //Modify values and insert them in the map mymap->insert(value); }
Thanks, that helps. I didn't know you could use void_allocators, I thought they had to be specific for each allocated type. One question: I noticed in the example you posted, that the lifetime of the allocator "alloc_inst" equals/exceeds the lifetime of the map and the values that are inserted into the map. Is this required, or can the allocator just be a temporary stack variable (as follows)? char_string key_object(void_allocator(segment.get_segment_manager()));
Jason Sachs wrote:
Thanks, that helps. I didn't know you could use void_allocators, I thought they had to be specific for each allocated type. One question: I noticed in the example you posted, that the lifetime of the allocator "alloc_inst" equals/exceeds the lifetime of the map and the values that are inserted into the map. Is this required, or can the allocator just be a temporary stack variable (as follows)?
char_string key_object(void_allocator(segment.get_segment_manager()));
Just like standard allocators the container makes an internal copy of the arguments (allocator or comparison function for associative containers), so you can initialize it with a variable in the stack. The goal is to create a container in shared memory and it will hold the allocator internally. Regards, Ion
participants (2)
-
Ion Gaztañaga
-
Jason Sachs