"Taking locks" the right way

Recall from Chapter 6, Smart Pointers, that one of the major problems of programs written in C and "old-style" C++ is the presence of pointer bugs--memory leaks, double-frees, and heap corruption--and that the way we eliminate those bugs from "new-style" C++ programs is via the use of RAII types such as std::unique_ptr<T>. Multi-threaded programming with raw mutexes have failure modes that are analogous to the failure modes of heap programming with raw pointers:

  • Lock leaks: You might take a lock on a particular mutex, and accidentally forget to write the code that frees it.
  • Lock leaks: You might have written that code, but due to an early return or an exception being thrown, the code never runs and the mutex ...

Get Mastering the C++17 STL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.