I have a multithreaded server application that needs mutex locks over some shared memory.
The shared memory are basically sTL maps etc.
Much of the time I\'m
You don't want boost-interprocess
if you're just using a single process. As the library name implies, it's used for Inter-Process Communication (IPC). You most likely want to use the boost-thread
mutex and locking concepts.
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
int
main()
{
typedef boost::shared_mutex Mutex;
typedef boost::shared_lock<Mutex> ReadLock;
typedef boost::unique_lock<Mutex> WriteLock;
Mutex mutex;
{
// acquire read lock
ReadLock read( mutex );
// do something to read resource
}
{
// acquire write lock
WriteLock write( mutex, boost::adopt_lock_t() );
// do something to write resource
}
}
there's a post on the boost mailing list explaining this as well.
You said that your application is multithreaded, so you should use boost::thread not boost::interprocess.
From the documentation (not tested) you should do it this way:
typedef boost::thread::shared_mutex shared_mutex;
boost::thread::upgrade_lock<shared_mutex> readLock(access_);
// Read access...
boost::thread::upgrade_to_unique_lock<shared_mutex> writeLock(readLock);
// Write access..
Also note that you acquire WRONG, sorry.it
while you lock for read access, so someone may delete this node and it's no longer valid when you get to the write section.
EDIT: I think that explanation in boost is clear. Let's try rephrase it to you anyway:
There are three main types of mutex concepts (I don't count TimedLockable since it's not related to your question):