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

What is wrong with this Producer-Consumer sample?

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 producer and in the consumers make the whole
app sleep
instead of only making the current thread sleep.
Maybe someone can help?

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ErzVerbr
{
class Program
{
private const int MAX_ELEMENTE = 100;
private const int MAX_VERBRAUCHER = 2;
private static int anzElemente = 0;
private static Stack<int> puffer = new Stack<int>(MAX_ELEMENTE);
private static Object elementeVorhanden = "";
private static Object platzFrei = "";
static void Main(string[] args)
{
puffer.Clear();
Thread erz = new Thread(new ThreadStart(erzeuge));
erz.Name = "Erzeuger";
erz.Start();
Thread[] verbr = new Thread[MAX_VERBRAUCHER];
for (int i = 0; i < MAX_VERBRAUCHER; i++)
{
verbr[i] = new Thread(new ThreadStart(verbrauche));
verbr[i].Name = "Verbraucher " + (i + 1);
verbr[i].Start();
}
Console.ReadKey();
}
static public void erzeuge()
{
while (true)
{
lock (elementeVorhanden)
{
if (anzElemente == MAX_ELEMENTE)
{
System.Console.WriteLine("Warte auf Platz...");
Monitor.Wait(platzFrei);
System.Console.WriteLine("Platz verfgbar!");
}
Random rand = new Random();
int element = rand.Next(10, 20);
puffer.Push(element);
Console.WriteLine("Element erzeugt: " +
element + ", Puffer belegt: " +
(anzElemente + 1));
anzElemente++;
Monitor.PulseAll(elementeVorhanden);
Console.WriteLine("Sleep: " + Thread.CurrentThread.Name);
Thread.Sleep(rand.Next(10, 50));
}
}
}
static public void verbrauche()
{
while (true)
{
lock (platzFrei)
{
if (anzElemente == 0)
{
System.Console.WriteLine("Warte auf Elemente...");
Monitor.Wait(elementeVorhanden);
System.Console.WriteLine("Elemente vorhanden!");
}
int element = puffer.Pop();
anzElemente--;
Console.WriteLine("Verbraucht: " + element);
Random rand = new Random();
Monitor.PulseAll(platzFrei);
Console.WriteLine("Sleep: " + Thread.CurrentThread.Name);
Thread.Sleep(rand.Next(2000, 5000));
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ErzVerbr
{
class Program
{
private const int MAX_ELEMENTE = 100;
private const int MAX_VERBRAUCHER = 2;
private static int anzElemente = 0;
private static Stack<int> puffer = new Stack<int>(MAX_ELEMENTE);
private static Object elementeVorhanden = "";
private static Object platzFrei = "";
static void Main(string[] args)
{
puffer.Clear();
Thread erz = new Thread(new ThreadStart(erzeuge));
erz.Name = "Erzeuger";
erz.Start();
Thread[] verbr = new Thread[MAX_VERBRAUCHER];
for (int i = 0; i < MAX_VERBRAUCHER; i++)
{
verbr[i] = new Thread(new ThreadStart(verbrauche));
verbr[i].Name = "Verbraucher " + (i + 1);
verbr[i].Start();
}
Console.ReadKey();
}
static public void erzeuge()
{
while (true)
{
lock (elementeVorhanden)
{
if (anzElemente == MAX_ELEMENTE)
{
System.Console.WriteLine("Warte auf Platz...");
Monitor.Wait(platzFrei);
System.Console.WriteLine("Platz verfgbar!");
}
Random rand = new Random();
int element = rand.Next(10, 20);
puffer.Push(element);
Console.WriteLine("Element erzeugt: " +
element + ", Puffer belegt: " +
(anzElemente + 1));
anzElemente++;
Monitor.PulseAll(elementeVorhanden);
Console.WriteLine("Sleep: " + Thread.CurrentThread.Name);
Thread.Sleep(rand.Next(10, 50));
}
}
}
static public void verbrauche()
{
while (true)
{
lock (platzFrei)
{
if (anzElemente == 0)
{
System.Console.WriteLine("Warte auf Elemente...");
Monitor.Wait(elementeVorhanden);
System.Console.WriteLine("Elemente vorhanden!");
}
int element = puffer.Pop();
anzElemente--;
Console.WriteLine("Verbraucht: " + element);
Random rand = new Random();
Monitor.PulseAll(platzFrei);
Console.WriteLine("Sleep: " + Thread.CurrentThread.Name);
Thread.Sleep(rand.Next(2000, 5000));
}
}
}
}
}
Mar 10 '06 #1
2 5460
Rene Ruppert wrote:
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 producer and in the consumers make the whole
app sleep instead of only making the current thread sleep.
Maybe someone can help?


You're sleeping while still holding the lock, so anything else waiting
to acquire the lock will have to wait until you've finished sleeping.

See http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml (about
half way down) for a sample producer/consumer queue.

Jon

Mar 10 '06 #2
> You're sleeping while still holding the lock, so anything else waiting
to acquire the lock will have to wait until you've finished sleeping.


Argl... thanks. That's it.

René
Mar 10 '06 #3

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

Similar topics

1
by: YoBro | last post by:
Lets say I wanted to select all employees and list their occupations. I have this query: SELECT * FROM employees, job WHERE employees.id=job.id Now some employees might have more than one job. I...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
11
by: Jasper Bryant-Greene | last post by:
MySQL 4.0.23-standard I run an online movie database with over 80,000 movies and over 500,000 people stored within. We use tables like `movies`, `people`, etc. for storing the actual movie...
6
by: CryingFreeman | last post by:
Hi everyone, I started in access just a copple of weeks ago, to automise my ecell DVD database.So I could make forms, ans reports.. Thought it would be easy, but I'm trying nog for weeks and I...
4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
9
by: David Teran | last post by:
Hi, we are currently using another database product but besides some licensing issues we are finding more and more problems with the database. We are evaluating PostgreSQL and it looks quite...
9
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif",...
7
by: brad | last post by:
url_queue = Queue.Queue(256) for subnet in subnets: url_queue.put(subnet) The problem is that I have 512 things to add to the queue, but my limit is half that... whoops. Shouldn't the...
25
by: Chris Thomasson | last post by:
The following code compiles fine with GCC, Comeau, EDG/C++ and VC++ 8/9: #include <cstdio> #include <string> int main() { { std::string names = { "One", "Two", "Three" };
3
by: lzo53 | last post by:
this is the code to a photogallery that displays thumbnails when clicked on a window opens my problem is i wish to customize the size of the window height 500 width 400 scrollbars no resize no and so...
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:
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
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
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.