3
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

Here's the picture of a Hoare-style monitor from wikipedia user Theodore.norvell:

room-style monitor analogy picture

  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).

  2. If the consumer sends the signal before the producer waits then the producer will be stuck forever. This is sometimes called "the missed wakeup problem." It is avoided by recognizing that you should really be waiting for some condition to become true (e.g., "the queue is no longer full"), not waiting for some condition variable to get signaled. When the producer is in the monitor (has acquired the mutex) it only waits on the condition variable if it sees that the condition is not currently true. The consumer signals the condition variable every time the condition (state) "queue is full" changes to "queue is not full", whether there is anyone waiting on the condition variable or not.

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.