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

Discussion in 'C++' started by music4, Aug 23, 2004.

  1. music4

    music4 Guest

    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
     
    music4, Aug 23, 2004
    #1
    1. Advertising

  2. music4

    Ron Natalie Guest

    "music4" <> wrote in message news:cgcc8i$...
    > 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.
     
    Ron Natalie, Aug 23, 2004
    #2
    1. Advertising

  3. music4

    Jerry Coffin Guest

    "music4" <> wrote in message news:<cgcc8i$>...
    > 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.
     
    Jerry Coffin, Aug 23, 2004
    #3
  4. music4

    Simon Turner Guest

    "music4" <> wrote in message news:<cgcc8i$>...
    > 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.
     
    Simon Turner, Aug 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B. Chernick
    Replies:
    0
    Views:
    269
    B. Chernick
    Dec 17, 2007
  2. Saraswati lakki
    Replies:
    0
    Views:
    1,347
    Saraswati lakki
    Jan 6, 2012
  3. Jack
    Replies:
    2
    Views:
    218
  4. Eric Wong
    Replies:
    0
    Views:
    208
    Eric Wong
    Feb 13, 2010
  5. Martin Sammtleben
    Replies:
    2
    Views:
    129
    Martin Sammtleben
    May 29, 2004
Loading...

Share This Page