我需要通过互斥锁保护资源。为了改进诊断,我正在考虑使用死锁警告timed_mutex
(代码未测试):
boost::timed_mutex m;
// first thread accessing the resource very frequently
while(...){
boost::mutex::scoped_lock(m);
// ...
}
// ...
// another thread accessing the resource, only occasionally
while(m.timed_lock(boost::get_system_time()+boost::posix_time::seconds(10)){
cerr<<"Waiting for lock for (additional) 10 seconds; deadlocked?"<<endl;
}
与两个循环timed_mutex
中简单的两个无条件锁相比,我是否会看到性能差异?mutex
(平台是 POSIX,以防万一)