Creating interprocess::shared_ptr across processes
Hi all, I've been looking into the "boost::interprocess" library to use shared memory the resides in a non-paged physical memory facilitated by RTX (Real-time Windows extension). According to my understanding it's best to use "managed_external_buffer". I have managed to create the shared memory and create and share objects using raw pointers. I would however prefer using smart_ptr that is offered by the boost::interprocess. The problem I'm having is that I can create the shared pointer for any shared object in one process but when I want to create it's counter-part in the other process, it does not share the "share_count" with the original shared_ptr in the first process. For instance I tried to share a list of integers. So I used the interprocess::list as "named" object and once I crated it (following that "interprocess" documentation) I placed that pointer into a interprocess::shared ptr that I created using the helper function "make_managed_shared_ptr". Now in another process I am able to get hold of the raw pointer to that same list by looking up by the name but cant figure out (did lots of searches about this to no avail) how to instantiate an interprocess::shared_ptr such that it would get the same shared_count object. When I use the "make_managed_shared_ptr" and pass in the raw pointer to the shared object it will create one but with a new "shared_count" which is obviously bad. First I thought that the "make_managed_shared_ptr" is able to resolve and find the already existing "shared_count" and link to that but figured it wasn't the case. Then I thought maybe I need to instantiate the shared_ptr as a named object and then retrieve the same object in another process but can't see the "make_managed_shared_ptr" would support such concept. Then I thought maybe the pre-implemented interprocess shareable types (e.g. vector, list, etc.) derive from "enable_shared_from_this" so I could just call the shared_from_this on the retrieved raw pointer to retrieve the shared_ptr that will be guaranteed to share the "shared_count". None of the above yielded the desired results. I didn't find (by googling) anybody talking about the process of creating a shared_ptr (using the helper method of "make_managed_shared_ptr") across the process so I assume that probably is clear and is able to achieve the mentioned results and I must be missing something awfully obvious. Can somebody please advise on how to achieve this? I am loath to just jump and implement my version of "make_managed_shared_ptr" unless I really have to and I'm sure it's designed to work across the processes. Thanks a in advance for help and guidance. Kind regards gxl
On Mon, 18 Apr 2011 05:10:36 +0200, GxL
Hi all,
I've been looking into the "boost::interprocess" library to use shared memory the resides in a non-paged physical memory facilitated by RTX (Real-time Windows extension). According to my understanding it's best to use "managed_external_buffer". I have managed to create the shared memory and create and share objects using raw pointers. I would however prefer using smart_ptr that is offered by the boost::interprocess.
When process dies unexpectedly nobody guarantees your destructors are called, but shared memory remains - this alone should let you rethink your idea. As a rule of thumb I'd advise placing only plain POD-composed structures in shared memory and never rely on the state of the shared memory on startup before some kind of handshake with the counterparts. -- Slava
When process dies unexpectedly nobody guarantees your destructors are called, but shared memory remains - this alone should let you rethink your idea. As a rule of thumb I'd advise placing only plain POD-composed structures in shared memory and never rely on the state of the shared memory on startup before some kind of handshake with the counterparts.
Thanks Slava. Good points and I'm providing for handshaking between the participants and not relying on the shared mem. states alone. While the point is valid about the unexpected death of the process, I still find a gap in the knowledge of how the interprocess::shared_ptr is expected to be used across the processes. On my side I've been first exploring/assessing how the interprocess Shared Memory works (including the usefulness of shared_ptr/weak_ptr) before making design decisions. I don't like making premature design decisions just because of some gap in understanding about some library - you know what I mean :). Nonetheless thanks for the points :). If there is anyone who can show me how to use interprocess::shared_ptr with managed_external_buffer and ensure that the two sides would share the same shared_count I would be very grateful. Thanks in advance. gxl
participants (2)
-
GxL
-
Viatcheslav.Sysoltsev@h-d-gmbh.de