3

我需要通过互斥锁保护资源。为了改进诊断,我正在考虑使用死锁警告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,以防万一)

4

1 回答 1

0

答案在于您的 pthread 库的实现。我不认为有很大的区别,但你能做的最好的就是测量它。

于 2012-12-11T21:44:28.700 回答