C
Cool Guy
I'm using a ProducerConsumer in a worker thread. If I want to end this
thread, and it's currently waiting for Consumer to return, what's the best
way to proceed?
Should I just make the thread a background thread? Or should I pass a
dummy object to ProducerConsumer.Produce to make Consume return? Or is
there a better way?
| public class ProducerConsumer
| {
| readonly object listLock = new object();
| Queue queue = new Queue();
|
| public void Produce(object o)
| {
| lock (listLock)
| {
| queue.Enqueue(o);
| if (queue.Count==1)
| {
| Monitor.Pulse(listLock);
| }
| }
| }
|
| public object Consume()
| {
| lock (listLock)
| {
| while (queue.Count==0)
| {
| Monitor.Wait(listLock);
| }
| return queue.Dequeue();
| }
| }
| }
thread, and it's currently waiting for Consumer to return, what's the best
way to proceed?
Should I just make the thread a background thread? Or should I pass a
dummy object to ProducerConsumer.Produce to make Consume return? Or is
there a better way?
| public class ProducerConsumer
| {
| readonly object listLock = new object();
| Queue queue = new Queue();
|
| public void Produce(object o)
| {
| lock (listLock)
| {
| queue.Enqueue(o);
| if (queue.Count==1)
| {
| Monitor.Pulse(listLock);
| }
| }
| }
|
| public object Consume()
| {
| lock (listLock)
| {
| while (queue.Count==0)
| {
| Monitor.Wait(listLock);
| }
| return queue.Dequeue();
| }
| }
| }