Click here to Skip to main content
15,892,222 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.8K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Threading;
using Zulu.Threading.ThreadPools.TaskItems;
using Zulu.Threading.ThreadPools.TaskQueues;

namespace Zulu.Threading.ThreadPools.TaskQueueControllers
{
    public abstract class TaskQueueController : ITaskQueueController
    {
        protected readonly object _locker = new object();
        protected readonly ITaskQueue _taskQueue;
        protected int _consumersWaiting;
        protected bool _isDisposed;

        protected TaskQueueController(ITaskQueue taskQueue)
        {
            if (taskQueue == null)
            {
                throw new ArgumentNullException("taskQueue");
            }
            _taskQueue = taskQueue;
        }

        public virtual int ConsumersWaiting
        {
            get
            {
                lock (_locker)
                {
                    return _consumersWaiting;
                }
            }
        }

        public virtual void Dispose()
        {
            lock (_locker)
            {
                if (_consumersWaiting > 0)
                {
                    GC.SuppressFinalize(this);
                    _isDisposed = true;
                    Monitor.PulseAll(_locker);
                }
            }
        }

        public IWorkItem Dequeue()
        {
            return DequeueCore();
        }

        public void Enqueue(IWorkItem item)
        {
            EnqueueCore(item);
        }

        protected abstract IWorkItem DequeueCore();
        protected abstract void EnqueueCore(IWorkItem item);
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions