[Src]ActionQueue

02/07/2012 20:01 I don't have a username#16
Quote:
Originally Posted by Fаng View Post
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.
02/07/2012 20:07 Spirited#17
Quote:
Originally Posted by I don't have a username View Post
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.
Yep. That's my action queue though. The only time that I'd use a single threaded queue is if I was outputting something to one, shared resource.
02/07/2012 20:29 I don't have a username#18
Quote:
Originally Posted by Fаng View Post
Yep. That's my action queue though. The only time that I'd use a single threaded queue is if I was outputting something to one, shared resource.
What about for stamina and so on? You have a thread for every player? o.o
02/07/2012 21:50 Korvacs#19
His is basically a threadpool, so its not 1 thread per player, they just share the work load.
02/08/2012 00:18 Spirited#20
Quote:
Originally Posted by Korvacs View Post
His is basically a threadpool, so its not 1 thread per player, they just share the work load.
Exactly. It's what I use for loading maps or managing high amounts of work.