Considering thread safety
Multithreaded programming in C++ is an entire book unto itself. Nonetheless, we have mentioned several situations throughout the book that potentially require the consideration of thread safety. It is worth re-iterating these topics to provide an overview of the issues you may encounter in various niches of C++ programming.
A program may be comprised of multiple threads, each of which may potentially compete against one another to access a shared resource. For example, a shared resource could be a file, socket, region of shared memory, or output buffer. Each thread accessing the shared resource needs to have carefully coordinated (known as mutually exclusive) access to the resource.
Imagine, for example, if two threads wanted to write output to your screen. If each thread could access the output buffer associated with cout
without waiting for the other to complete a cohesive statement, the output would be a garbled mess of random letters and symbols...