Multiple Waiters
This example shows one of the functions available with a semaphore beyond that available with MVS Wait and Post. ECBs permit only one waiter at a time, whereas semaphores may have multiple threads waiting concurrently.
Figure 1 shows three threads in a process doing Wait/Post processing using a Semaphore.
The steps are:
- Thread Wait1 creates semaphore S with process level scope, the initial value for use as a wait/post mechanism, and saves the handle of the semaphore in order that the other threads can use it.
- Thread Wait1 creates Thread Wait2 that also waits for the console to be read.
- Thread Wait1 creates Thread Post to read the console.
- Thread Wait1 issues the SemWait function, which decrements the value of semaphore S by 1. Because the value of semaphore S is now -1, Thread Wait1 is put on the queue for semaphore S.
- Thread Wait2 issues the SemWait function, which decrements the value of semaphore S by 1. Because the value of semaphore S is now -2, Thread Wait2 is put on the queue for semaphore S.
- Thread Post waits until something is entered from the console. The console input is saved in a shared buffer.
- After reading the console, Thread Post signals semaphore S, unblocking Thread Wait1.
- Thread Post loops back and waits for something else to be entered from the console.
- Thread Wait1 gets the console input from the shared buffer and processes it.
- Thread Wait1 loops back and waits for another line to be read from console.
- Thread Post reads the console and signals semaphore S, unblocking Thread Wait2. Thread Wait2 now gets the console input from the shared buffer and processes it.
- Thread Wait2 loops back and waits for another line to be read from console.