From the course: Parallel and Concurrent Programming with C++ Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Mutual exclusion

Mutual exclusion - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 1

Start my 1-month free trial

Mutual exclusion

- Anytime multiple threads are concurrently reading and writing a shared resource, it creates the potential for incorrect behavior, like a data race. But we can defend against that by identifying and protecting critical sections of code. A critical section, or critical region, is part of a program that accesses a shared resource, such as a data structured memory, or an external device, and it may not operate correctly if multiple threads concurrently access it. The critical section needs to be protected so that it only allows one thread or process to execute in it at a time. - Baron and I experienced a data race as we added garlic to our shared shopping list, because incrementing a value is actually a three-step process. Read the current value, modify it, and then write back the result. Those three steps are a critical section, and they need to execute as an uninterrupted action, so we don't accidentally overwrite each…

Contents