January 2020
Intermediate to advanced
454 pages
11h 25m
English
Up until this point, our synchronization primitives have serialized access to our shared resource. That is, each thread must execute one at a time when accessing the critical region. Although this ensures corruption is not possible, it is inefficient for certain types of scenarios. To better understand this, we must examine what causes corruption in the first place.
Let's consider an integer variable that is incremented by two threads simultaneously. The process for incrementing an integer variable is as follows: i = i + 1.
Let's write this as follows:
int i = 0;auto tmp = i;tmp++;i = tmp; // i == 1
To prevent corruption, we use a mutex to ensure that if two threads increment the integer, they do so synchronously:
auto tmp_thread1 ...