]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/example/comp_doc_anonymous_mutexB.cpp
1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/interprocess/detail/config_begin.hpp>
11 //[doc_anonymous_mutexB
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 "doc_anonymous_mutex_shared_data.hpp"
19 using namespace boost::interprocess
;
23 //Remove shared memory on destruction
26 ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
32 //Open the shared memory object.
33 shared_memory_object shm
34 (open_only
//only create
35 ,"MySharedMemory" //name
36 ,read_write
//read-write mode
39 //Map the whole shared memory in this process
42 ,read_write
//Map it as read-write
45 //Get the address of the mapped region
46 void * addr
= region
.get_address();
48 //Construct the shared structure in memory
49 shared_memory_log
* data
= static_cast<shared_memory_log
*>(addr
);
52 for(int i
= 0; i
< 100; ++i
){
54 scoped_lock
<interprocess_mutex
> lock(data
->mutex
);
55 std::sprintf(data
->items
[(data
->current_line
++) % shared_memory_log::NumItems
]
56 ,"%s_%d", "process_a", i
);
57 if(i
== (shared_memory_log::NumItems
-1))
59 //Mutex is released here
62 //Wait until the other process ends
64 scoped_lock
<interprocess_mutex
> lock(data
->mutex
);
71 #include <boost/interprocess/detail/config_end.hpp>