Здравствуйте!
Написал код который реализует producer consumer паттерн c помощью ConcurrentQueue<T>(необходима именно fifo).
Меня смущает то как я решил эту задачу, прошу Вас указать на нюансы и показать как бы Вы реализовали это.
class Program
{
static bool DestroyThread = false;
static ConcurrentQueue<int> MyQueue = new ConcurrentQueue<int>();
static AutoResetEvent AutoResetEvent = new AutoResetEvent(false);
static Thread MyThread = null;
static void Main(string[] args)
{
MyThread = new Thread(new ThreadStart(ThreadWork));
MyThread.Start();
for (int i = 0; i < 100; i++)
{
MyQueue.Enqueue(i);
AutoResetEvent.Set();
}
DestroyThread = true;
AutoResetEvent.Set();
}
static void ThreadWork()
{
while (!DestroyThread)
{
if (MyQueue.Count > 0)
{
int temp;
MyQueue.TryDequeue(out temp);
Console.WriteLine(temp);
}
else
AutoResetEvent.WaitOne();
}
}
}
if (MyQueue.Count > 0)такое чувство, что можно сделать более элегантно. – K.Oleg Nov 25 '17 at 09:36