There are a few caveats to putting objects into shared memory. The most important ones all deal with pointers:
That's not to say you shouldn't try... Just try carefully and test a lot!
// page04.html,v 1.7 2000/11/27 17:56:44 othman Exp #include "shmem.h" #include "ace/Log_Msg.h" #if defined (ACE_LACKS_SYSV_SHMEM) int main (int, char *[]) { ACE_ERROR_RETURN ((LM_ERROR, "System V Shared Memory not available on this platform\n"), 100); } #else // ACE_LACKS_SYSV_SHMEM int main (int, char *argv[]) { // Be sure the segment is sized to hold our object. ACE_Shared_Memory_SV shm_server (SHM_KEY, sizeof (SharedData), ACE_Shared_Memory_SV::ACE_CREATE); char *shm = (char *) shm_server.malloc (); if (shm == 0) ACE_ERROR_RETURN ((LM_ERROR, "%p\n\t(%P|%t) Cannot create shared memory segment.\n" "\tUse 'ipcs' to see if it already exists\n", argv[0]), 100); ACE_DEBUG ((LM_INFO, "(%P|%t) Shared Memory is at 0x%x\n", shm )); /* Use the placement new syntax to stuff the object into the correct location. I think they generally reserve this for the advanced class... */ SharedData *sd = new (shm) SharedData; // Use the set() method to put some data into the object sd->set (); // Set the 'available' flag to zero so that we can wait on it sd->available (0); /* Another cheesy busy loop while we wait for the object to become available. The cool way would be to hide a semaphore or two behind this method call & eliminate the sleep. */ while (sd->available () == 0) ACE_OS::sleep (1); // Show the user what's in the segment sd->show (); // All done. if (shm_server.remove () < 0) ACE_ERROR ((LM_ERROR, "%p\n", "remove")); return 0; } #endif /* ACE_LACKS_SYSV_SHMEM */
// page04.html,v 1.7 2000/11/27 17:56:44 othman Exp #include "shmem.h" #include "ace/Log_Msg.h" #if defined(ACE_LACKS_SYSV_SHMEM) int main (int, char *[]) { ACE_ERROR_RETURN ((LM_ERROR, "System V Shared Memory not available on this platform\n"), 100); } #else // ACE_LACKS_SYSV_SHMEM int main (int, char *[]) { ACE_Shared_Memory_SV shm_client (SHM_KEY, sizeof (SharedData)); char *shm = (char *) shm_client.malloc (); ACE_DEBUG ((LM_INFO, "(%P|%t) Shared Memory is at 0x%x\n", shm)); /* More placement new. The constructor parameter prevents clobbering what the server may have written with it's show() method. */ SharedData *sd = new (shm) SharedData (0); // Show it sd->show (); // Change it sd->set (); // Advertise it sd->available (1); shm_client.close (); return 0; } #endif /* ACE_LACKS_SYSV_SHMEM */