473,396 Members | 1,961 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,396 software developers and data experts.

Single producer and Multiple consumers

I have a single producer that produce something. I have n consumers (in n threads). The producer knows the number of consumers. The producer must produce 1 unit of data, make the n consumers start and wait the n consumers to end. Then it must produce another unit of data...
How should I do it? I'm trying with ManualResetEvent and AutoResetEvent but I have some race conditions... For example I tried something like:

public class Producer {
public AutoResetEvent ProducerWait;
public ManualResetEvent ConsumersWait;
public int RemainingConsumers;
}

The basic idea was that the Producer set the RemainingConsumer to the number of threads, reset the ProducerWait, SignalAndWait(ConsumersWait, ProducerWait). (so it set the ConsumerWait and wait for the ProducerWait to be set).
The Consumer(s) WaitOne for the ConsumerWait, do the elaboration it should decrement RemainingConsumers and if RemainingConsumers is 0 it should set the ProducerWait and wait for...

And here there is the problem...

I could Reset the ConsumerWait (so that I can wait for the next set of ConsumerWait), but then it could create a race condition (if I have two consumers, and the first one Reset the ConsumerWait before the second one has started, the second one doesn't start).

Can anyone help me? Is there another solution? Thanks!
Mar 3 '10 #1
0 1958

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Debaser | last post by:
I'm getting an IllegalMonitorStateException in trying to implement the above subject. The static declararions made it work, so if this is part of the problem let me know. I think it has something...
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...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
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: 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...
2
by: dwhall | last post by:
I have 2 python scripts: examples of a producer and a filter, respectively: #! /usr/bin/env python import sys, time if __name__ == "__main__": while True: sys.stdout.write("hello.\r\n")...
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: Jean-Paul Calderone | last post by:
On Tue, 10 Jun 2008 22:46:37 -0700 (PDT), George Sakkis <george.sakkis@gmail.comwrote: This isn't true. Access is synchronized automatically by virtue of the fact that there is no pre-emptive...
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?
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
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...
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.