Share Data Safely with Mutex and Locks
Learn to share data safely with mutex and locks.
We'll cover the following
The term mutex refers to mutually exclusive access to shared resources. A mutex is commonly used to avoid data corruption and race conditions due to multiple threads of execution attempting to access the same data. A mutex will typically use locks to restrict access to one thread at a time.
The STL provides mutex and lock classes in the <mutex>
header.
How to do it
In this recipe, we will use a simple Animal
class to experiment with locking and unlocking a mutex
:
We start by creating a
mutex
object:
Get hands-on with 1400+ tech skills courses.