June 2017
Intermediate to advanced
532 pages
12h 59m
English
Another interesting use case is the limited scope of critical sections. Consider the following example:
if (std::lock_guard<std::mutex> lg {my_mutex}; some_condition) { // Do something}
At first, an std::lock_guard is created. This is a class that accepts a mutex argument as a constructor argument. It locks the mutex in its constructor, and when it runs out of scope, it unlocks it again in its destructor. This way, it is impossible to forget to unlock the mutex. Before C++17, a pair of extra braces was needed in order to determine the scope where it unlocks again.
Yet another interesting use case is the scope of weak pointers. Consider the following:
if (auto shared_pointer (weak_pointer.lock()); shared_pointer != nullptr) ...Read now
Unlock full access