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.

Mutexes

Mutex stands for mutual exclusion. It ensures that only one thread can access a critical ...

Access this course and 1400+ top-rated courses and projects.