Hi Mike,
As for the AutoResetEvent docs I assume that you're referring to this:
"There is no guarantee that every call to the Set method will release a
thread. If two calls are too close together, so that the second call occurs
before a thread has been released, only one thread is released. It is as if
the second call did not happen."
"AutoResetEvent Class"
http://msdn2.microsoft.com/en-us/lib...esetevent.aspx
I think it means that two serial calls to Set may only release one thread if
they are executed too close together (time-wise).
If you're referring to context switches, I don't think that calling Set on
an EventWaitHandle automatically causes a context switch, especially on a
single processor machine. Set signals blocking threads that they can run
whenever Windows decides to try and let them. That could be immediately, or
whenever Windows decides to interrupt the current process, AFAIK.
The example is written for one and only one consumer thread, which waits
until either one of the _newItemEvent or _exitThreadEvent EventWaitHandles
is set.
The code guarantees that there will be at least one item on the queue
whenever the consumer attempts to dequeue one. This is because the
_newItemEvent is set by the producer after enqueueing an item, and the
consumer only attempts to dequeue one item at a time, waiting on
_newItemEvent before trying again. When Windows allows the consumer to run,
whenever that may be, there will always be at least one item to be dequeued.
--
Dave Sexton
"mps" <mp*@discussions.microsoft.comwrote in message
news:B4**********************************@microsof t.com...
It seems to me that the MSDN code for synchronizing a producer and
consumer
thread at http://msdn2.microsoft.com/en-us/library/yy12yx1f.aspx is
completely wrong. There is no reason to assume that each call to
NewItemEvent.Set() will release a consumer thread (as explicitly mentioned
in
the AutoResetEven documentation), which means that the consumer threads
won't
necessarily know that there are items on the queue.
Is this correct?
Mike