Example 1 Solution
Example 1 Solution
Example 1 Solution
A preferred method for solving the problem in the example above is to avoid the dependency between the data and flag values. You can do this by using a more robust thread synchronization scheme. Although you can employ many of the thread synchronization techniques, one that lends itself well to this problem is a semaphore.
In order for the following logic to be appropriate, you must assume the following:
- The program created the semaphore before starting the cooperating threads.
- The program initialized the semaphore to a count of 1.
Initializing Thread All Other Threads
------------------- ---------------------------
DATA = 10 Wait for semaphore count to reach 0
Decrement semaphore use DATA