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_conditionB 12 #include <boost/interprocess/shared_memory_object.hpp> 13 #include <boost/interprocess/mapped_region.hpp> 14 #include <boost/interprocess/sync/scoped_lock.hpp> 15 #include <iostream> 16 #include <cstring> 17 #include "doc_anonymous_condition_shared_data.hpp" 18 19 using namespace boost::interprocess; 20 main()21int main () 22 { 23 //Create a shared memory object. 24 shared_memory_object shm 25 (open_only //only create 26 ,"MySharedMemory" //name 27 ,read_write //read-write mode 28 ); 29 30 try{ 31 //Map the whole shared memory in this process 32 mapped_region region 33 (shm //What to map 34 ,read_write //Map it as read-write 35 ); 36 37 //Get the address of the mapped region 38 void * addr = region.get_address(); 39 40 //Obtain a pointer to the shared structure 41 trace_queue * data = static_cast<trace_queue*>(addr); 42 43 //Print messages until the other process marks the end 44 bool end_loop = false; 45 do{ 46 scoped_lock<interprocess_mutex> lock(data->mutex); 47 if(!data->message_in){ 48 data->cond_empty.wait(lock); 49 } 50 if(std::strcmp(data->items, "last message") == 0){ 51 end_loop = true; 52 } 53 else{ 54 //Print the message 55 std::cout << data->items << std::endl; 56 //Notify the other process that the buffer is empty 57 data->message_in = false; 58 data->cond_full.notify_one(); 59 } 60 } 61 while(!end_loop); 62 } 63 catch(interprocess_exception &ex){ 64 std::cout << ex.what() << std::endl; 65 return 1; 66 } 67 68 return 0; 69 } 70 //] 71 #include <boost/interprocess/detail/config_end.hpp> 72