ACE Tutorial 020
Sharing your Memories with persistence


I wanted to show placement new again & prove that you can use it with a memory mapped file just as easily as with a shared memory segment.

Imagine if you had an object that contained an image & then you mapped that to a file... Instead of a bunch of jpg files laying around, you would actually have objects instead. Save the image? No problem, it's already there!



server2.cpp


// $Id$

#include "mmap.h"

#include "ace/Log_Msg.h"

int
main (int, char *[])
{
    ACE_Shared_Memory_MM shm_server (SHM_KEY, sizeof(SharedData) );

    char *shm = (char *) shm_server.malloc ();

    ACE_DEBUG ((LM_INFO, "(%P|%t) Memory Mapped file is at 0x%x\n",
                shm ));

    SharedData * sd = new(shm) SharedData;

    sd->set();
    sd->available(0);

    while ( ! sd->available() )
        ACE_OS::sleep (1);

    sd->show();

    if (shm_server.remove () < 0)
        ACE_ERROR ((LM_ERROR, "%p\n", "remove"));

    return 0;
}


client2.cpp


// $Id$

#include "mmap.h"

#include "ace/Log_Msg.h"

int main (int, char *[])
{
    ACE_Shared_Memory_MM shm_client (SHM_KEY, sizeof(SharedData));

    char *shm = (char *) shm_client.malloc ();

    ACE_DEBUG ((LM_INFO, "(%P|%t) Memory Mapped file is at 0x%x\n",
                shm ));

    SharedData * sd = new(shm) SharedData(0);

    sd->show();
    sd->set();
    sd->available(1);

    shm_client.close();

    return 0;
}


[Tutorial Index] [Continue This Tutorial]