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:

  1. 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.
  2. Thread Wait1 creates Thread Wait2 that also waits for the console to be read.
  3. Thread Wait1 creates Thread Post to read the console.
  4. 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.
  5. 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.
  6. Thread Post waits until something is entered from the console. The console input is saved in a shared buffer.
  7. After reading the console, Thread Post signals semaphore S, unblocking Thread Wait1.
  8. Thread Post loops back and waits for something else to be entered from the console.
  9. Thread Wait1 gets the console input from the shared buffer and processes it.
  10. Thread Wait1 loops back and waits for another line to be read from console.
  11. 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.
  12. Thread Wait2 loops back and waits for another line to be read from console.
Figure 1. Multiple Waiters Using a Semaphore
Thread 'Wait1'               Thread 'Wait2'              Thread 'Post'
--------------               --------------              -------------

1 SemCreate
    sem_name=S
    scope_of_sem=PROCESS
    init_val_sem=0
    --------
    sem_handle=HS
    rc=OK

2  Create thread "Wait2"

3  Create thread "Post"

4  SemWait
     semaphore_handle=HS
     --------
     rc=OK

                             5  SemWait
                                  semaphore_handle=HS
                                  --------
                                  rc=OK

                                                        6  Read console and
                                                           save input in
                                                           shared buffer

                                                        7  SemSignal
                                                             sem_handle=HS
                                                             --------
                                                             rc=OK

                                                        8  Loop back to 6

9  Get the console input
   from the shared buffer
   and process it

10 Loop back to 4

                             11 Get the console input
                                from the shared buffer
                                and process it

                             12 Loop back to 5