#include #include #include #include #include #include using namespace boost::interprocess; int main() { // Remove any shared memory from a previous test run. shared_memory_object::remove("sharedMemTest"); named_mutex::remove("sharedMemTestMutex"); // Create a second copy of this process so that we have two processes // modifying the shared memory. We don't care which is the parent and // which is the child, since they will be doing the same operations. int child_pid = fork(); try { managed_shared_memory shared(open_or_create, "sharedMemTest", 2048); named_mutex mutex(open_or_create, "sharedMemTestMutex"); for (int i = 0; i < 10000; ++i) { { #if EXTRA_MUTEX scoped_lock lock(mutex); #endif shared.find_or_construct("key")(); } { #if EXTRA_MUTEX scoped_lock lock(mutex); #endif shared.destroy("key"); } } } catch (const std::exception & e) { std::cerr << "Unhandled exception: " << e.what() << std::endl; } // Since the shell will not print the exit status of the child process, // print it ourselves. if (child_pid) { int child_status; waitpid(child_pid, &child_status, 0); if (WIFSIGNALED(child_status)) { std::cerr << "child exited with signal " << WTERMSIG(child_status) << std::endl; } else { std::cout << "Test passed." << std::endl; } } return 0; }