473,395 Members | 2,437 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,395 software developers and data experts.

Is this producer/consumer model stable?

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 noticing that every once in a while my program simply hangs- it is
rare but it is annoying. I am worried it may be a synchronization issue
involving my producer/consumer threads.

I basically go like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. AutoResetEvent autoEvent = new AutoResetEvent(false);
  3. Queue q = new Queue();
  4.  
  5. private void AddToken (Token t)
  6. {
  7.  
  8. lock (q)
  9. {
  10. q.Enqueue(t);
  11. if (q.Count == 1) { autoEvent.Set(); }
  12. }
  13.  
  14. }
  15.  
  16. public bool HasMoreTokens ()
  17. {
  18. if (status < StatusType.DONE && q.Count == 0)
  19. {
  20. autoEvent.WaitOne(); autoEvent.Reset();
  21. }
  22. return (q.Count > 0);
  23. }
  24.  
  25. public Token NextToken ()
  26. {
  27. while (status < StatusType.DONE && q.Count == 0)
  28. {
  29. autoEvent.WaitOne(); autoEvent.Reset();
  30. }
  31. lock (q)
  32. {
  33. return (Token)q.Dequeue();
  34. }
  35. }
  36.  
  37.  
So the basic idea here is my producer makes token objects which get pushed
onto a Queue using the AddToken method. If the queue was empty, it sets the
AutoResetEvent so any waiting consumer thread is awakened.

A consumer thread would either call HasMoreTokens or NextToken which will
wait if the status is not 'DONE' (aka the producer is still working) and
the queue is empty. Once a token on the queue is available, it will get
awoken by the producer.

Does this look flawed?
Nov 16 '05 #1
3 4933
MrNobody <Mr******@discussions.microsoft.com> wrote:
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 noticing that every once in a while my program simply hangs- it is
rare but it is annoying. I am worried it may be a synchronization issue
involving my producer/consumer threads.


There are a few problems in your code:

1) You should *always* lock the queue when you use it.
2) Your access to status isn't thread-safe
3) You should do the waiting and the dequeuing within a single lock
4) You don't need to call Reset on the autoEvent - that's what the
AutoReset part is there for :) Currently there's a race condition
there. I would actually suggest using Wait/Pulse instead of
AutoResetEvent, but that's just me.
5) Calling HasMoreTokens can "eat" the signal which another thread
could be waiting for. I would personally ditch the HasMoreTokens
method, making NextToken just return null if the status becomes
DONE while there are no items in the queue.

See http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for my
own producer/consumer class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Thanks Jon,

I have ditched the mess I had and implemented something similar to your
example. Now there's only a NextToken method and the thread handling IO will
put an EOF when done Token so the consumer will know to stop when it reads
such a token, so that whole status flag was dumped as well.

The only difference is I don't create a lock object, instead I just lock the
queue itself. Is there any reason why you do not lock the queue instead of
using a lock object?
Nov 16 '05 #3
MrNobody <Mr******@discussions.microsoft.com> wrote:
I have ditched the mess I had and implemented something similar to your
example. Now there's only a NextToken method and the thread handling IO will
put an EOF when done Token so the consumer will know to stop when it reads
such a token, so that whole status flag was dumped as well.
Good - I was thinking overnight about the best way of signalling an
end, and had come up with the same solution. The only difficulty is
that if you have more than one consumer thread, you need to know how
many EOF tokens to put in the queue.
The only difference is I don't create a lock object, instead I just lock the
queue itself. Is there any reason why you do not lock the queue instead of
using a lock object?


In this case it would be okay to lock on the queue, as every time I use
the queue I'm already locking it, and nothing else has access to it.
However, in general I prefer to have separate private references so
that I can guarantee that the only code locking it is the code I write.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

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...
2
by: ian douglas | last post by:
I have one process that will be multi-threaded. The parent (A) will sit and deal with TCP/IP issues, and feed data to its child process (B) via shared memory. I need assistance in finding a good...
3
by: smith4894 | last post by:
Hello, I have an application that essentially consists of two threads doing their things. One thread is a producer, and pushes bytes (of a struct) into a pipe, and another is a consumer that...
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: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is...
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.
4
by: mps | last post by:
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...
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...
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
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
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.