Is there any library class I can use for a buffer in a consumer-producer situation with multiple threads?
I don’t very well understand the multithreading ways of C# so thew example of a perfect solution is in Java:
//Thread 1
Buffer buf = new Buffer();
Thread t2 = new Thread(new MyRunnable(buf) );
while(true){
buf.put(foo);
}
}
//MyRunnable
private Buffer buf;
public MyRunnable(Buffer buf){
this.buf = buf;
}
public void run(){
while(!buf.IsFinished()){
foo = buf.take();
dosth(foo);
}
}
System.Collection.Concurrenthas a number of implementations of theIProducerConsumerCollection<T>interface (e.g.ConcurrentQueue<T>), which may be of use in your situation.There is also a
BlockingCollection<T>class that lets your thread block while waiting for the input.