リソースをミューテックスで保護する必要があります。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;
}
シンプルなin both ループtimed_mutex
の 2 つの無条件ロックと比較すると、パフォーマンスの違いが見られるでしょうか? mutex
(違いが生じる場合に備えて、プラットフォームはPOSIXです)