473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Producer/Consumer

mps
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
Dec 9 '06 #1
4 11139
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

Dec 10 '06 #2
mps
That is what I'm referring to. I agree that there will always be at least one
item to be dequeued when a thread is woken up, so the dequeue will never
fail. However, it is perfectly possible that all threads will be sleeping
even though there remain items in the queue (because the number of thread
wake-ups that occur may be less than the number of times the event is set).
The bottom-line is this is a completely busted implementation of a core
multithreading algorithm. Kind of embarrassing to see that up on MSDN. Makes
me wonder about the quality of the threading code in MS products :/

Mike

"Dave Sexton" wrote:
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


Dec 10 '06 #3
Mike,

Yep, I agree. That's one of the poorest examples of the
producer-consumer pattern I've seen.

Take a look at the producer-consumer example in the following article
for a correct version. It has been peer reviewed extensively.

http://www.yoda.arachsys.com/csharp/...eadlocks.shtml

Brian

mps wrote:
That is what I'm referring to. I agree that there will always be at least one
item to be dequeued when a thread is woken up, so the dequeue will never
fail. However, it is perfectly possible that all threads will be sleeping
even though there remain items in the queue (because the number of thread
wake-ups that occur may be less than the number of times the event is set).
The bottom-line is this is a completely busted implementation of a core
multithreading algorithm. Kind of embarrassing to see that up on MSDN. Makes
me wonder about the quality of the threading code in MS products :/

Mike
Dec 10 '06 #4
Hi Mike,

Yes, I see what you mean now, though the problem isn't with Set. The
problem is that the producer queues 20 items at a time as an atomic
operation, but the consumer is only prepared to dequeue one at a time.
Dequeuing 20 as such would fix that problem:

while (_queue.Count 0)
_queue.Dequeue();

A potential problem is that the EventWaitHandle used to exit the application
can interrupt the consumer, leaving items to remain in the queue, but that
may be acceptable depending upon the requirements of the application.

Anyway, Jon Skeet's implementation seems much better [link posted by Brian].
Using Monitor.Pulse makes sense and the code is more legible :)

--
Dave Sexton

"mps" <mp*@discussions.microsoft.comwrote in message
news:58**********************************@microsof t.com...
That is what I'm referring to. I agree that there will always be at least
one
item to be dequeued when a thread is woken up, so the dequeue will never
fail. However, it is perfectly possible that all threads will be sleeping
even though there remain items in the queue (because the number of thread
wake-ups that occur may be less than the number of times the event is
set).
The bottom-line is this is a completely busted implementation of a core
multithreading algorithm. Kind of embarrassing to see that up on MSDN.
Makes
me wonder about the quality of the threading code in MS products :/

Mike

"Dave Sexton" wrote:
>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**********************************@microso ft.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



Dec 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dh | last post by:
Runtime.getRuntime().exec() ... mad buffering of stdout ... It buffers stdout without limit ... so if your Java program doesn't keep up with the Process output then memory fills up! I wish it...
7
by: Evan Simpson | last post by:
WEBoggle needs a new game board every three minutes. Boards take an unpredictable (much less than 3min, but non-trivial) amount of time to generate. The system is driven by web requests, and I...
3
by: MrNobody | last post by:
I am developing an application that has several multi threaded tasks where one thread is doing IO and another thread is grabbing data from the first thread to process it further. I've been...
0
by: Kyle Rowe | last post by:
class Buffer { const int size = 4; int n = 0; public void Put(char ch) { lock(this) { while (n == size) Monitor.Wait(this);
2
by: Rene Ruppert | last post by:
Hi, I'm trying to implement the Producer-Consumer-Problem in C#. Below is my code. The problem is, that the buffer always contains only one element...it seems that the Thread.Sleep() in the...
4
by: cpptutor2000 | last post by:
Could someone please help me? I am looking for a C language implementation of the producer consumer model. Any help would be greatly appreciated. Thanks in advance for your help.
1
by: Eihab | last post by:
how to write a message based producer consumer program for linux with four processes . the reader process will read an input file , one line at a time . reader will take the each line of the input...
10
by: George Sakkis | last post by:
I'd like some feedback on a solution to a variant of the producer- consumer problem. My first few attempts turned out to deadlock occasionally; this one seems to be deadlock-free so far but I can't...
1
by: akevan | last post by:
Hi all, I'm writing a cross-platform audio processing app that should run on various hardware platforms. Some of these have OSs that provide software threads, some have hardware threads, but some are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.