Exercise 2: Working with the critical_section class
Figure 1
By now, you should be aware that shared variables between can have negative effects on your application. When working with shared data, you can synchronize data access by primarily using:
In this exercise you will learn how to use the critical_section blocking method to make sure that data access is synchronized between threads. Part 1: Running the Unsynchronized Code
Part 2: Synchronizing with critical_section
Figure 3
Note:
Run the program various times to make sure that the correct result is shown.The critical_section lock is now making sure that only one of the threads can modify the number variable; thus the program will not have any data races.
|
DownloadsDownload The Offline Training Kit Contents
|