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>
12 //[doc_anonymous_upgradable_mutexB
13 #include <boost/interprocess/shared_memory_object.hpp>
14 #include <boost/interprocess/mapped_region.hpp>
15 #include <boost/interprocess/sync/scoped_lock.hpp>
16 #include "doc_upgradable_mutex_shared_data.hpp"
20 using namespace boost::interprocess;
24 //Remove shared memory on destruction
27 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
30 //Open the shared memory object.
31 shared_memory_object shm
32 (open_only //only create
33 ,"MySharedMemory" //name
34 ,read_write //read-write mode
37 //Map the whole shared memory in this process
40 ,read_write //Map it as read-write
43 //Get the address of the mapped region
44 void * addr = region.get_address();
46 //Construct the shared structure in memory
47 shared_data * data = static_cast<shared_data*>(addr);
50 for(int i = 0; i < 100; ++i){
51 //Lock the upgradable_mutex
52 scoped_lock<interprocess_upgradable_mutex> lock(data->upgradable_mutex);
53 std::sprintf(data->items[(data->current_line++) % shared_data::NumItems]
54 ,"%s_%d", "process_a", i);
55 if(i == (shared_data::NumItems-1))
57 //Mutex is released here
60 //Wait until the other process ends
62 scoped_lock<interprocess_upgradable_mutex> lock(data->upgradable_mutex);
70 #include <boost/interprocess/detail/config_end.hpp>