我有一个需要审查的基本示例(C++)。
假设我有一个函数 PublicFunc(),还有一个叫做 PrivateFunc()。我想仔细同步它们。但是 PrivateFunc 有时也可以调用 PublicFunc,这意味着我们从同一个线程调用它。这会导致阻塞,我想解决它。
mutable boost::mutex m;
void PublicFunc() {
m.lock();
//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
m.unlock();
}
void PrivateFunc() {
m.lock();
PublicFunc();
OtherPrivateFunc();
m.unlock();
}
boost 库中的哪个互斥锁或锁是正确的?谢谢!