A process that waits on multiple events must be carefully designed or there is a possibility that some events might be overlooked. This possibility arises because the value returned by the wait statement always indicates the leftmost of the events in the event list that has been caused. For example, consider the following ALGOL statement:
ENUM:= WAIT (E1, E2, E3);
If E1 is caused, ENUM receives a value of 1. If E1 and E2 are caused, ENUM still receives a value of 1. Now, suppose that E1 is an event that happens very frequently. Each time the wait statement is executed and E1 has already happened, the wait statement returns 1 as a value; thus, the process might never be notified that event E2 or E3 has happened. This situation is referred to as a starvation problem.
Strictly speaking, a starvation problem exists only if the repeated wait statement is not fulfilling the needs of the particular application. The effect of the wait statement is to give preference to the leftmost events in the event list. But if the leftmost events occur infrequently, there will be no starvation. If you order the list so that the most important events are on the left, then the starvation condition might even be desirable.
If the events in a wait list are equally important, you might wish to consider using a start index in the wait statement. A start index indicates which of the events in a wait list should be tested first. You can design the program to change the value of the start index before each time the wait statement is executed. Start indexes are supported only in ALGOL and COBOL85. The following is an ALGOL example using start indexes:
EVORDER:= 1;
WHILE NOT DONE DO
BEGIN
EVT:= WAIT (EV1, EV2, EV3, EV4) [EVORDER];
EVORDER:= * + 1;
IF EVORDER > 4 THEN
EVORDER:= 1;
.
.
.
END;Another way to prevent events from being overlooked is to use happened tests after each execution of the wait statement. You could apply a happened test to each event that is to the right of the event that was returned by the wait statement. The following is an ALGOL example of a procedure that uses this technique:
PROCEDURE EVENTWAIT;
BEGIN
BOOLEAN BOOL;
INTEGER ENUM;
DO BEGIN
ENUM:= WAIT (E1, E2, E3);
CASE ENUM OF
BEGIN
1: BOOL:= INPUTHANDLER (TRUE, HAPPENED(E2), HAPPENED(E3));
2: BOOL:= INPUTHANDLER (FALSE, TRUE, HAPPENED(E3));
3: BOOL:= INPUTHANDLER (FALSE, FALSE, TRUE);
END;
END
UNTIL BOOL;
END EVENTWAIT;The procedure EVENTWAIT is responsible for waiting on three events, E1, E2, and E3, which were declared globally. When at least one of these events is caused, EVENTWAIT invokes another procedure called INPUTHANDLER and passes it Boolean values indicating whether each of the three events has been caused. The ENUM value indicates the leftmost event that has happened. The happened test is used for each of the events to the right of that event. You increase efficiency by minimizing the number of happened tests.
INPUTHANDLER is expected to make whatever response is appropriate for each event. INPUTHANDLER returns a Boolean value of TRUE if there is no need to wait on any more events. INPUTHANDLER is also expected to reset the events that were caused, so that it will be meaningful to wait on them again.
Note that the INPUTHANDLER invocation is used in this example for the sake of simplicity. From an efficiency standpoint, such repeated procedure invocations are rather expensive. It would be better to include the code that handles each event in the EVENTWAIT procedure.

