using namespace boost::interprocess;//Let's create any mutex type:
MutexType mutex;{//This will lock the mutexscoped_lock<MutexType> lock(mutex);//Some code//The mutex will be unlocked here
}{//This will try_lock the mutexscoped_lock<MutexType> lock(mutex, try_to_lock);//Check if the mutex has been successfully lockedif(lock){//Some code}//If the mutex was locked it will be unlocked
}{boost::posix_time::ptime abs_time = ...//This will timed_lock the mutexscoped_lock<MutexType> lock(mutex, abs_time);//Check if the mutex has been successfully lockedif(lock){//Some code}//If the mutex was locked it will be unlocked
}
For more information, check the?scoped_lock's reference.
#include <boost/interprocess/sync/interprocess_mutex.hpp>struct shared_memory_log
{enum { NumItems = 100 };enum { LineSize = 100 };shared_memory_log(): current_line(0), end_a(false), end_b(false){}//Mutex to protect access to the queueboost::interprocess::interprocess_mutex mutex;//Items to fillchar items[NumItems][LineSize];int current_line;bool end_a;bool end_b;
};
這是進程主進程。創建共享內存,構建循環緩沖區,并開始寫軌跡。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include "doc_anonymous_mutex_shared_data.hpp"
#include <iostream>
#include <cstdio>using namespace boost::interprocess;int main ()
{try{//Remove shared memory on construction and destructionstruct 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 //only create,"MySharedMemory" //name,read_write //read-write mode);//Set sizeshm.truncate(sizeof(shared_memory_log));//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memoryshared_memory_log * data = new (addr) shared_memory_log;//Write some logsfor(int i = 0; i < shared_memory_log::NumItems; ++i){//Lock the mutexscoped_lock<interprocess_mutex> lock(data->mutex);std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems],"%s_%d", "process_a", i);if(i == (shared_memory_log::NumItems-1))data->end_a = true;//Mutex is released here}//Wait until the other process endswhile(1){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->end_b)break;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
第二個進程打開共享內存,獲得對循環緩沖區的訪問權,并開始寫入痕跡。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include "../include/doc_anonymous_mutex_shared_data.hpp"
#include <iostream>
#include <cstdio>using namespace boost::interprocess;int main ()
{//Remove shared memory on destructionstruct shm_remove{~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Open the shared memory object.shared_memory_object shm(open_only //only create,"MySharedMemory" //name,read_write //read-write mode);//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memoryshared_memory_log * data = static_cast<shared_memory_log*>(addr);//Write some logsfor(int i = 0; i < 100; ++i){//Lock the mutexscoped_lock<interprocess_mutex> lock(data->mutex);std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems],"%s_%d", "process_a", i);if(i == (shared_memory_log::NumItems-1))data->end_b = true;//Mutex is released here}//Wait until the other process endswhile(1){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->end_a)break;}return 0;
}