Boost:Interprocess
lastmod 2015-07-03created 2015-06-232 edits
Shared memory, memory mapped files, process-shared mutexes, condition variables, containers and allocators.
scoped_lock
boost::mutex::scoped_lock is an object that holds a lock using a mutex within the scope in which the object is alive (scoped). Usage is as follows:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
using namespace boost;
int nGlobalNum = 0;
void ThFunc(boost::mutex *pMutex)
{
int i = 0;
for (i = 0; i < 10; i++)
{
boost::mutex::scoped_lock lock(*pMutex);
cout << "thread (" << this_thread::get_id() << ") " << nGlobalNum << "\n";
this_thread::sleep(boost::posix_time::millisec(500));
nGlobalNum++;
}
}
int main(int argc, char **argv)
{
boost::mutex mtx;
boost::thread_group tg;
tg.create_thread(boost::bind(ThFunc, &mtx));
tg.add_thread(new boost::thread(boost::bind(ThFunc, &mtx)));
tg.join_all();
return 0;
}
Favorite site
-
Chapter 12. Boost.Interprocess - Chapter 14. Boost.Interprocess
- boost::mutex example
- Interprocess Communication, Shared Memory