Problems may happen when multiple threads try to access a resource. For a simple example, suppose we have a thread that adds one to a variable. It does this by first reading the variable, adding one to it, then storing it back. Suppose we initialize this variable to 1, then create two instances of this thread. After both threads finish, intuition suggests that this variable should have a value of 3. However, the below table illustrates what might go wrong:

| Thread 1 | Thread 2 |

|———––|———————–|———————–| | Time Step 1 | Read 1 from variable | | | Time Step 2 | | Read 1 from variable | | Time Step 3 | Add 1 plus 1 to get 2 | | | Time Step 4 | | Add 1 plus 1 to get 2 | | Time Step 5 | Store 2 into variable | | | Time Step 6 | | Store 2 into variable |

As you can see, at the end of the operation, 2 is in the variable, instead of 3. The reason is that Thread 2 read the variable before Thread 1 was done updating it. The solution? Mutexes.

A mutex (portmanteau of mutual exclusion) is a resource management object designed to solve this type of problem. When a thread wants to access a resource, it “acquires” the resource’s mutex. Once it is done accessing the resource, the thread “releases” the mutex. While the mutex is acquired, all calls to acquire the mutex will not return until the mutex is released. To better understand this, think of a mutex as a waiting line at the supermarket: the threads go into line by trying to acquire the mutex and then waiting for the threads ahead of them to finish up, then using the resource, then stepping out of line by releasing the mutex. There would be complete pandemonium if everybody tried to access the resource at once.

std::mutex is C++11’s implementation of a mutex.

#include <thread>
#include <mutex>
#include <iostream>
using namespace std;

void add_1(int& i, const mutex& m) { // function to be run in thread
    i += 1;

int main() {
    int var = 1;
    mutex m;

    cout << var << endl; // prints 1
    thread t1(add_1, var, m); // create thread with arguments
    thread t2(add_1, var, m); // create another thread
    t1.join(); t2.join(); // wait for both threads to finish
    cout << var << endl; // prints 3