473,372 Members | 1,056 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,372 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 11135
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.