我需要通过互斥锁来保护资源。为了改善诊断,我正在考虑使用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,如果有所作为)
答案 0 :(得分:0)
答案是关于pthread库的实现。我认为没有太大区别,但你能做的最好的就是测量它。