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

Unlock the full course today

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

Mutual exclusion: C++ demo

Mutual exclusion: C++ demo - C++ Tutorial

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

Start my 1-month free trial

Mutual exclusion: C++ demo

- [Instructor] To demonstrate how to manually enforce mutual exclusion with a Mutex, we'll modify the example program from earlier with two shopper's that have a data race, as it can currently increment the amount of garlic to buy. Before creating a Mutex, I'll need to include the Mutex header in this program. Now, I'll create a new Mutex, and because Barron and I used the pencil for our Mutex, I'll give it the creative name of pencil. To keep the two shopper threads from modifying the garlic count and variable at the same time, I'll call the pencil Mutexes Lock Number function to acquire the lock on it before entering the for-loop on line 11. And, I'll call its unlock method after the for-loop completes, to release the hold on it. Switching over to the command prompt, I'll build that program with a Make Command and then run it. The first shopper thread to begin executing will take the pencil, increment the garlic count…

Contents