boost::mutex和boost::timed_mutex的性能差异

performance difference of boost::mutex and boost::timed_mutex

本文关键字:boost mutex 性能 timed      更新时间:2023-10-16

我需要通过互斥来保护资源。为了改进诊断,我正在考虑使用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;
}

当与两个循环中的简单mutex的两个无条件锁相比时,我会看到timed_mutex的性能差异吗?(平台是POSIX,以防产生影响)

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