Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it possible to put a class object in share memory?

Reply
Thread Tools

Is it possible to put a class object in share memory?

 
 
music4
Guest
Posts: n/a
 
      08-23-2004
Greeting,

My platform is Solaris 2.8. My question is that if there is a simple way to
put a class object in share memory, so that multiple process can use this
object.

Thanks in advance!
Evan


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      08-23-2004

"music4" <(E-Mail Removed)> wrote in message news:cgcc8i$(E-Mail Removed)...
> Greeting,
>
> My platform is Solaris 2.8. My question is that if there is a simple way to
> put a class object in share memory, so that multiple process can use this
> object.


Look up functions called shm* in the Solaris docs.

However, it's actually NOT easy for any but the most trivial classes
to share memories as there are pointers both explicit and hidden inside
class objects that may not be valid in different processes.

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      08-23-2004
"music4" <(E-Mail Removed)> wrote in message news:<cgcc8i$(E-Mail Removed)>...
> Greeting,
>
> My platform is Solaris 2.8. My question is that if there is a simple way to
> put a class object in share memory, so that multiple process can use this
> object.


The usual way is to allocate the shared memory, then use the placement
new operator to construct an object there. If you have a type of
object that is always shared, you can instead overload its operator
new to allocate shared memory.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Simon Turner
Guest
Posts: n/a
 
      08-23-2004
"music4" <(E-Mail Removed)> wrote in message news:<cgcc8i$(E-Mail Removed)>...
> Greeting,
>
> My platform is Solaris 2.8. My question is that if there is a simple way to
> put a class object in share memory, so that multiple process can use this
> object.
>
> Thanks in advance!
> Evan


The direct way is to allocate[1] a chunk of that shared memory for
your object, and then use placement new[2] to initialise the instance
in that memory.

The user-friendly-but-more-work way[3] is to write an operator new for
the class which takes as an extra argument the shared memory
'arena'[4] you want the object created in.


Footnotes:
[1] - you need some allocator for your shared memory which provides
the equivalent of new and delete functions for allocating/releasing
chunks of that shared segment. This might be entirely trivial if you
only ever want an array of one type of object, but it could be a
full-fledged heap implementation.

[2] - see the C++ FAQ or your favourite reference if you haven't used
this - it initialises an object in some raw memory.

[3] - this is only really friendlier if you're allocating these things
all over the place, and don't want to change the client code (much).
If you're using a factory function, there isn't much point doing this.

[4] - this could just be a pointer to the start of your shared memory
segment if your allocation routine is simple and well-known, or it
could be a pointer to an allocator object (see [1] again) which
provides a known allocation interface, where some allocator instance
is associated with a particular shm segment. See the C++ FAQ (again) -
section 11.14.


Caveats:
Note that if your objects aren't POD, you'll have to make sure that
all their subobjects allocate from the same shared memory segment.
For things like std::string, that means the typedef breaks, since you
need to pass std::basic_string a different allocator.

Also, none of this will work for objects having virtual functions,
because the vtable pointer will hold an address specific to the
process which created the object.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Is it possible for 2 Object Data Sources to share a table adapter? B. Chernick ASP .Net 0 12-17-2007 02:30 PM
question DRb class definition share or not share Jack Ruby 2 10-11-2007 05:26 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Getting all elements that share the same class - possible? Martin Sammtleben Javascript 2 05-29-2004 05:15 PM



Advertisments