Take the 2-minute tour ×

I have a basic sample which needs review (C++).

Let's say I have a function PublicFunc(), and another one called PrivateFunc(). I'd like to synchronize them carefully. But PrivateFunc can sometimes call PublicFunc as well what means we are calling it from the same thread. This causes blocks, and I'd like to solve it.

mutable boost::mutex m;

void PublicFunc() {
 //Here it blocks, but why?
 //What I need is to get the lock if this func was called from PrivateFunc(), so exactly from the same thread.
 //But! It should definitely block on calling PublicFunc from outside while we are for example in the 'OtherPrivateFunc'.

 //Do some stuff

 //this is not necessary

void PrivateFunc() {




Which mutex or lock is the right one from the boost library? Thank you!

share|improve this question
add comment

1 Answer

A mutex may only be locked once; any call to lock the mutex while it is locked will block, even if the attempt to lock the mutex is made by the thread that holds the lock on the mutex.

If you want to be able to lock a mutex multiple times on the same thread, use recursive_mutex.

Alternatively, consider reorganizing your code so that you have one set of (private) member functions that assume the mutex is locked, and have all other functions delegate to these. This can make the code clearer and can make it easier to verify that the synchronization is correct.

share|improve this answer
add comment

Your Answer


