c++ - doubt about way to lock a boost mutex -
c++ - doubt about way to lock a boost mutex -
boost::recursive_mutex m; m.lock();
versus
boost::lock_guard<boost::recursive_mutex> lock( mutex_ ); is there advantage utilize first form? sec form provide raii mecanism, or there others advantages ?
the advantage of using lock_guard release lock when goes out of scope. eliminates need manually release lock , reduces chance of forgetting so.
boost::recursive_mutex mylock; { boost::lock_guard<boost::recursive_mutex> lock( mylock ); // if(false == do_something()) { return; // "lock" goes out of scope , unlocks 'mylock' it's destructor. } } // "lock" has gone out of scope , unlocked 'mylock' it's destructor. c++ boost mutex
Comments
Post a Comment