try catch with locks in C++

  • A+
Category:Languages

In java:

Lock lock = new ReentrantLock(); try{    lock.lock();   someFunctionLikelytoCauseAnException(); }  catch(e){...}  finally {    lock.unlock(); } 

My question is with this above example we know that the lock WILL always get unlocked because finally always executes, but what is the guarantee with C++?

mutex m; m.lock(); someFunctionLikelytoCauseAnException(); /// ???? 

How will this work and why?

 


For this we use the RAII-style construct std::lock_guard. When you use

std::mutex m; { // start of some scope     std::lock_guard lg(m);     // stuff } // end of scope 

lg will ensure that m will be unlocked no matter what path the scope is left as it is destroyed at scope exit and std::lock_guards destructor will call unlock

Even if an exception is thrown the stack will be unwound (stack unwinding) and that process destroys lg which in turn will call unlock guaranteeing that the lock is released.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: