In this recipe, we will learn how to make a class thread-safe (that is, how to ensure a class's public member functions can be called at any time, by any number of threads simultaneously). Most classes, especially those provided by the C++ standard library are not thread-safe and, instead, assume the user will add thread-synchronization primitives such as an std::mutex object as needed. The problem with this approach is that every object has two instances that must be tracked in code: the class itself and its std::mutex. The user must also wrap each of the object's functions with custom versions that protect the class using std::mutex, resulting in not only two objects that must be managed, but also a bunch of C-style wrapper functions.
This recipe is important because it will demonstrate how to address these issues in your code by making a...