# Hoare's monitor - what happens after a “signal”

Assume I have producer and consumer in a monitor.

1. The code of the consumer signals the producer and then does something else. I know in a Hoare-style monitor, after signaling, the thread that is signaled wakes up and is is now schedule. What about the other instructions in the consumer after the signal? When will the consumer continue?

2. What if the consumer sends the signal before the producer does the wait? Will the producer be stuck forever?

3. If there is a context switch will the mutex of the monitor be released?

1. The circles are threads, the rooms off to the sides are various queues. Think of the monitor as being protected by a mutex. Only one thread gets the mutex at a time. Once a thread has the mutex it keeps the mutex until it releases the mutex by (1) waiting (in which case it goes to sleep on a condition variable), (2) signalling (in which case it goes into the queue labeled s in the picture or (3) exiting the monitor completely. The scheduling algorithm is given in the wikipedia article. At any point in time the relative priorities to get into the monitor are: (1) the single thread that was most recently signalled, (2) threads in queue s (usually s is FIFO), (3) threads in queue e (the entry queue).