I kind of new in multi thread programming. So in my case I have a boost multi_index
container which can be reached from many threads simultaneously. I'm performing some search and insert operations.
So when a search starts with an index I don't want a new value to be inserted by another thread. Because it can change the index and mass things up. So I have to use a mutex.
So many people using boost scoped_lock
for this purpose. My quesition is simply what is the "scope" of scoped_lock
?
Assume that I have a function like that:
void A ()
{
myData data;
// prepare data here
// ******* 1 ********
B(data); // assume this perform some operations
}
void B (myData data)
{
// do some stuff with data
// ******* 2 ********
search(data);
}
void search(myData data)
{
// ******* 3 ********
// start searching the data here
}
so I would like to acquire the lock from the beginning of the process it means from procedure A. If I put my boost::mutex::scoped_locklock(mutex);
code to the place which is marked as ******* 1 ********
does it lock also the processes in procedure B
and procedure search
or do I have to put lock inside B
and search
as well? (to the places marked with 2 & 3). Which is the right place 1, 2, 3 or all?
By the way my app is single writer & multiple reader type. So shared_lock
makes to much difference in my case or is it ok to go with scoped_lock
?
Note: I'm using visual c++ in visual sturdio 2008 environment
Thanks...