1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Copyright (C) 2014 Vicente Botet
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/thread.hpp>
using namespace boost::interprocess;
struct item
{
int i;
};
struct queue
{
void put( const item& item )
{
boost::unique_lock<boost::mutex> lock(mutex);
while ( item_in )
cond_full.wait(lock);
item_ = item;
item_in = true;
cond_empty.notify_one();
}
void print()
{
boost::unique_lock<boost::mutex> lock(mutex);
while ( !item_in )
cond_empty.wait(lock);
item_in = false;
std::cout << item_.i << std::endl;
cond_full.notify_one();
}
private:
//Mutex to protect access to the queue
boost::mutex mutex;
//Condition to wait when the queue is empty
boost::condition_variable cond_empty;
//Condition to wait when the queue is full
boost::condition_variable cond_full;
bool item_in;
//Items to fill
item item_;
};
void *addr;
void printThread()
{
//Erase previous shared memory and schedule erasure on exit
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
//Create a shared memory object.
shared_memory_object shm(create_only,"MySharedMemory",read_write);
try
{
// //Set size
// shm.truncate(sizeof(queue));
//
// //Map the whole shared memory in this process
// mapped_region region(shm,read_write);
//
// //Get the address of the mapped region
// void *addr = region.get_address();
//Construct the shared structure in memory
queue *q = new (addr) queue;
do
{
q->print();
} while ( true );
}
// catch(interprocess_exception &ex)
// {
// std::cout << ex.what() << std::endl;
// }
catch(boost::thread_interrupted&)
{
std::cout << "interrupted" << std::endl;
}
catch(...)
{
std::cout << "exception" << std::endl;
}
}
int main()
{
addr = new queue();
boost::thread t(printThread);
// give the thread time to create the shm
boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
// //Create a shared memory object.
// shared_memory_object shm(open_only,"MySharedMemory",read_write);
try
{
// //Map the whole shared memory in this process
// mapped_region region(shm,read_write);
//
// //Get the address of the mapped region
// void *addr = region.get_address();
//Obtain a pointer to the shared structure
queue *q = static_cast<queue*>(addr);
item i;
i.i = 42;
q->put( i );
++i.i;
q->put( i );
// give the printThread time to "process" the item
boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
t.interrupt();
t.join();
}
catch(...)
{
std::cout << "exception" << std::endl;
return -1;
}
}
|