Quote:
Originally Posted by Fаng
Nah. This is mine:
Code:
public sealed class ThreadedQueue
{
// Configuration:
private readonly object _locker;
private Thread[] _workers;
private Queue<Action> _queue;
public ThreadedQueue(int workerCount)
{
_locker = new object();
_queue = new Queue<Action>();
_workers = new Thread[workerCount];
for (int i = 0; i < workerCount; i++)
(_workers[i] = new Thread(Dequeue)).Start();
}
private void Dequeue()
{
while (true)
{
Action obj;
lock (_locker)
{
while (_queue.Count == 0)
Monitor.Wait(_locker);
obj = _queue.Dequeue();
}
if (obj == null)
return;
obj();
}
}
public void Enqueue(Action method)
{
lock (_locker)
{
_queue.Enqueue(method);
Monitor.Pulse(_locker);
}
}
public void Shutdown(bool waitForWorkers)
{
foreach (Thread worker in _workers)
Enqueue(null);
if (waitForWorkers)
foreach (Thread worker in _workers)
worker.Join();
_workers = null;
_queue = null;
}
}
|
Yours are not doing same thing as mine tho. The wrapper you have is for a thread queue with a lot threads to handle, mine is one thread with a queue of objects to handle.