Exemplo n.º 1
0
        /// <summary>Initialize the thread pool.</summary>
        public WorkerThreadPool(int maxThreadCount)
        {
            // Create our thread stores; we handle synchronization ourself
            // as we may run into situtations where multiple operations need to be atomic.
            // We keep track of the threads we've created just for good measure; not actually
            // needed for any core functionality.
            this.maxWorkerThreads = maxThreadCount;
            workerQueue = new Queue<WorkerCallback>();
            workerThreads = new List<Thread>();
            waitCount = new SemaphoreCount(0);

            // Create our "thread needed" event
            workerThreadsSemaphore = new Semaphore(0, this.maxWorkerThreads);

            // Create all of the worker threads
            for (int i = 0; i < this.maxWorkerThreads; i++)
            {
                // Create a new thread and add it to the list of threads.
                Thread newThread = new Thread(new ThreadStart(ProcessQueuedItems));
                workerThreads.Add(newThread);

                // Configure the new thread and start it
                newThread.Name = "WorkerThreadPool #" + i.ToString();
                newThread.IsBackground = true;
                newThread.Start();
            }
        }
Exemplo n.º 2
0
        /// <summary>Initialize the thread pool.</summary>
        public WorkerThreadPool(int maxThreadCount)
        {
            // Create our thread stores; we handle synchronization ourself
            // as we may run into situtations where multiple operations need to be atomic.
            // We keep track of the threads we've created just for good measure; not actually
            // needed for any core functionality.
            this.maxWorkerThreads = maxThreadCount;
            workerQueue           = new Queue <WorkerCallback>();
            workerThreads         = new List <Thread>();
            waitCount             = new SemaphoreCount(0);

            // Create our "thread needed" event
            workerThreadsSemaphore = new Semaphore(0, this.maxWorkerThreads);

            // Create all of the worker threads
            for (int i = 0; i < this.maxWorkerThreads; i++)
            {
                // Create a new thread and add it to the list of threads.
                Thread newThread = new Thread(new ThreadStart(ProcessQueuedItems));
                workerThreads.Add(newThread);

                // Configure the new thread and start it
                newThread.Name         = "WorkerThreadPool #" + i.ToString();
                newThread.IsBackground = true;
                newThread.Start();
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 构造一个初始为给定集合的实例。
 /// </summary>
 public Semaphore(IEnumerable <T> collection)
 {
     foreach (var data in collection)
     {
         Data.AddLast(data);
         SemaphoreCount.Release(1);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// 产生一个对象。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public void Release(T data)
        {
            lock (Data)
            {
                Data.AddLast(data);
            }

            SemaphoreCount.Release();
        }
Exemplo n.º 5
0
        /// <summary>
        /// 获取一个对象。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public async Task <T> WaitAsync(int timeout = -1)
        {
            if (!await SemaphoreCount.WaitAsync(timeout, TokenSource.Token).ConfigureAwait(false))
            {
                throw new TimeoutException();
            }

            lock (Data)
            {
                T retval = Data.First.Value;
                Data.RemoveFirst();
                return(retval);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 获取一个对象。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public T Wait(int timeout = -1)
        {
            if (!SemaphoreCount.Wait(timeout, TokenSource.Token))
            {
                throw new TimeoutException();
            }

            lock (Data)
            {
                T retval = Data.First.Value;
                Data.RemoveFirst();
                return(retval);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 产生一组对象。
        /// </summary>
        /// <exception cref="Exception"></exception>
        /// <returns></returns>
        public void Release(IEnumerable <T> collection)
        {
            int count = 0;

            lock (Data)
            {
                foreach (var data in collection)
                {
                    Data.AddLast(data);
                    count++;
                }
            }

            SemaphoreCount.Release(count);
        }
Exemplo n.º 8
0
        public void Dispose()
        {
            if (!IsDisposed)
            {
                IsDisposed = true;

                try
                {
                    TokenSource.Cancel();
                }
                catch { }
                TokenSource.Dispose();
                SemaphoreCount.Dispose();

                lock (Data)
                {
                    Data.Clear();
                }
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// 构造一个初始为给定元素的实例。
 /// </summary>
 public Semaphore(T data)
 {
     Data.AddLast(data);
     SemaphoreCount.Release(1);
 }