简体   繁体   中英

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

I need to protect a resource by mutex. In order to improve diagnosis, I am thinking about deadlock warning using timed_mutex (code not tested):

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;
}

Am I going to see a performance difference with timed_mutex when compared with two unconditional locks of a simple mutex in both loops? (The platform is POSIX, in case that makes a difference)

The answer is on the implementation of your pthread library. I don't think there is a big difference, but the best you can do is to measure it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM