Mutexes

In this lesson, let's look at how to tackle data races with mutexes.

Shared Data

We only need to think about synchronization if we have shared mutable data since this kind of data is prone to data races.

As previously stated, when the program has a data race, it has undefined behavior. Undefined behavior affects the program before and after the undefined behavior, causing many problems for the programmer and the program. When our program has undefined behavior, the C++ community says:

When the program has a catch-fire semantic, even the computer can catch fire 🔥

The easiest way to visualize concurrent, unsynchronized read and write operations is to write something to std::cout.

A mutex (mutual exclusion) guarantees that at most one thread has access to the critical region.

Get hands-on with 1200+ tech skills courses.