1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2006-2012. 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) 6 // 7 // See http://www.boost.org/libs/interprocess for documentation. 8 // 9 ////////////////////////////////////////////////////////////////////////////// 10 #include <boost/interprocess/detail/config_begin.hpp> 11 //[doc_anonymous_semaphoreB 12 #include <boost/interprocess/shared_memory_object.hpp> 13 #include <boost/interprocess/mapped_region.hpp> 14 #include <iostream> 15 #include "doc_anonymous_semaphore_shared_data.hpp" 16 17 using namespace boost::interprocess; 18 main()19int main () 20 { 21 //Remove shared memory on destruction 22 struct shm_remove 23 { 24 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } 25 } remover; 26 //<- 27 (void)remover; 28 //-> 29 30 //Create a shared memory object. 31 shared_memory_object shm 32 (open_only //only create 33 ,"MySharedMemory" //name 34 ,read_write //read-write mode 35 ); 36 37 //Map the whole shared memory in this process 38 mapped_region region 39 (shm //What to map 40 ,read_write //Map it as read-write 41 ); 42 43 //Get the address of the mapped region 44 void * addr = region.get_address(); 45 46 //Obtain the shared structure 47 shared_memory_buffer * data = static_cast<shared_memory_buffer*>(addr); 48 49 const int NumMsg = 100; 50 51 int extracted_data [NumMsg]; 52 //<- 53 (void)extracted_data; 54 //-> 55 56 //Extract the data 57 for(int i = 0; i < NumMsg; ++i){ 58 data->nstored.wait(); 59 data->mutex.wait(); 60 extracted_data[i] = data->items[i % shared_memory_buffer::NumItems]; 61 data->mutex.post(); 62 data->nempty.post(); 63 } 64 return 0; 65 } 66 //] 67 #include <boost/interprocess/detail/config_end.hpp> 68