1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 //[doc_anonymous_conditionA
12 #include <boost/interprocess/shared_memory_object.hpp>
13 #include <boost/interprocess/mapped_region.hpp>
14 #include <boost/interprocess/sync/scoped_lock.hpp>
17 #include "doc_anonymous_condition_shared_data.hpp"
19 using namespace boost::interprocess;
24 //Erase previous shared memory and schedule erasure on exit
27 shm_remove() { shared_memory_object::remove("MySharedMemory"); }
28 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
31 //Create a shared memory object.
32 shared_memory_object shm
33 (create_only //only create
34 ,"MySharedMemory" //name
35 ,read_write //read-write mode
39 shm.truncate(sizeof(trace_queue));
41 //Map the whole shared memory in this process
44 ,read_write //Map it as read-write
47 //Get the address of the mapped region
48 void * addr = region.get_address();
50 //Construct the shared structure in memory
51 trace_queue * data = new (addr) trace_queue;
53 const int NumMsg = 100;
55 for(int i = 0; i < NumMsg; ++i){
56 scoped_lock<interprocess_mutex> lock(data->mutex);
58 data->cond_full.wait(lock);
61 std::sprintf(data->items, "%s", "last message");
63 std::sprintf(data->items, "%s_%d", "my_trace", i);
65 //Notify to the other process that there is a message
66 data->cond_empty.notify_one();
68 //Mark message buffer as full
69 data->message_in = true;
72 catch(interprocess_exception &ex){
73 std::cout << ex.what() << std::endl;