gpt4 book ai didi

c++ - boost::mutex 和 boost::timed_mutex 的性能差异

转载 作者:太空狗 更新时间:2023-10-29 23:18:54 27 4
gpt4 key购买 nike

我需要通过互斥来保护资源。为了改进诊断,我正在考虑使用 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 库的实现。我不认为有什么大的不同,但你能做的最好的事情就是测量它。

关于c++ - boost::mutex 和 boost::timed_mutex 的性能差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com