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庫中正確的? 謝謝!