views:

45

answers:

1

If so, that would imply that the blocked thread can enter when another thread calls Monitor.Wait(obj). This seems a little odd to me in that it must contend with other threads in the ready queue.

If not, can it only un-block when Monitor.Exit(obj) is called? Or is it in obj's waiting queue?

This isn't clearly documented in the MSDN Library class documentation.

+1  A: 

Yes, the blocked thread can enter when another thread calls Monitor.Wait. Why would it not be able to?

In particular, it has to be able to - as the thread which is currently blocking may be the one which is going to call Pulse.

Consider a producer/consumer queue:

Producer                Consumer

                        Enter lock
Enter lock (block)
                        Check: queue is empty
                        Wait (block)
Unblocked
Add item to queue
Pulse
Exit lock
                        Unblocked (enters lock again)
                        Check: queue is now not empty
                        Fetch item
                        Exit lock

This scenario wouldn't work if the producer thread were still waiting for an Exit call.

Jon Skeet
I see, the example basically proves that if you're even gonna support Wait/Pulse, then blocking on Enter requires going to ready since Wait/Pulse can only be called between calls to Enter and Exit. But, since threading is complex enough and I don't have access to the source, I also expect the documentation to explicitly explain Enter in terms of the queues. Thank you for confirming and explaining.
gWiz