4
我有一个需要评论的基本示例(C++)。Boost为同一个线程获得多个锁
比方说,我有一个函数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库中正确的? 谢谢!