Non-trivial examples of Boost::Interprocess library usage
Hi I'm new to Boost, and also new to Boost::Interprocess. I've been reading through the documentation for Boost::Interprocess, and have successfully written an application which creates a tree like structure in a managed_memory_mapped_file; saves it to disk, and then other processes can reopen that mmap'ed file and traverse the tree. However, I feel like I really am not making the best use of the library, and would like to get more experience by reading other example code from significant applications Does anyone have source code of a non-trivial application that exercises shared memory or memory-mapped files, storing objects with pointers to other objects (and strings) into the shared memory, deleting, updating, retrieving them etc. Specifically, say I'm trying to port an existing heap-based structure into shared memory. struct foo { int x, y; std::vector< std::string > keys; struct foo* left, right; } struct foo* root; What would this translate to in the boost::interprocess world? Is it possible to write templatized versions of these data structures that would work either with the existing std::allocator heap-based memory or with boost::interprocess stateful allocators without needing to create two versions of this data structure? One problem I've encountered is that default constructors for boost::interprocess::vector/string etc are not possible since they need to be constructed with a stateful allocator... so, does this mean that any constructor for object foo requires an allocator to be passed in which is then used in the initializer list to initialize these member containers? Sorry if this doesn't make sense - but any help would be appreciated. Thanks Evan. ----------------------------------------- E> evan.torrie@yahoo.com
Hi, I've used shmem the predecessor to interprocess in an application: http://myweb.tiscali.co.uk/janstetka/ http://myweb.tiscali.co.uk/janstetka/dl_src.html http://myweb.tiscali.co.uk/janstetka/interprocess_map.html I hope this is of some help.
Hi, Evan Torrie wrote:
[snip]
Specifically, say I'm trying to port an existing heap-based structure into shared memory.
struct foo { int x, y; std::vector< std::string > keys; struct foo* left, right; }
struct foo* root;
What would this translate to in the boost::interprocess world?
Something like:
struct foo
{
typedef boost::interprocess::allocator<char> char_allocator_t;
typedef boost::interprocess::
basic_string
Is it possible to write templatized versions of these data structures that would work either with the existing std::allocator heap-based memory or with boost::interprocess stateful allocators without needing to create two versions of this data structure?
Boost Interprocess containers can work with std allocators (code not
compiled, it will surely contain errors):
//This is provided by boost 1.34
#include
One problem I've encountered is that default constructors for boost::interprocess::vector/string etc are not possible since they need to be constructed with a stateful allocator... so, does this mean that any constructor for object foo requires an allocator to be passed in which is then used in the initializer list to initialize these member containers?
Yes. There is no way to have a default constructor, because the allocator needs the address of the shared memory segment and you can have several managed_shared_memory objects around. Hope this helps, Ion
participants (3)
-
Evan Torrie
-
Ion Gaztañaga
-
Jan