Posts

Showing posts with the label locking

Try catch with locks in C++

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. what is the guarantee with C++? The relevant guarantee in C++ works a bit differently...