Ejemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ThreadPool" /> class.
        /// </summary>
        public ThreadPool(int numberOfThreads, string threadsNamePrefix)
        {
            NumberOfThreads = numberOfThreads;
            threads         = new Queue <SingleThreadRunner>();
            threadsIdle     = new ConcurrentQueue <SingleThreadRunner>();

            // allocate threads...
            for (var i = 0; i < NumberOfThreads; i++)
            {
                var singleThreadRunner = new SingleThreadRunner(this);
                singleThreadRunner.Thread              = new Thread(singleThreadRunner.DoWork);
                singleThreadRunner.Thread.Name         = threadsNamePrefix + (i + 1);
                singleThreadRunner.Thread.IsBackground = true;

                threads.Enqueue(singleThreadRunner);
                threadsIdle.Enqueue(singleThreadRunner);

                singleThreadRunner.Thread.Start();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ThreadPool" /> class.
        /// </summary>
        public ThreadPool(int numberOfThreads, string threadsNamePrefix)
        {
            NumberOfThreads = numberOfThreads;
            threads = new Queue<SingleThreadRunner>();
            threadsIdle = new ConcurrentQueue<SingleThreadRunner>();

            // allocate threads...
            for (var i = 0; i < NumberOfThreads; i++)
            {
                var singleThreadRunner = new SingleThreadRunner(this);
                singleThreadRunner.Thread = new Thread(singleThreadRunner.DoWork);
                singleThreadRunner.Thread.Name = threadsNamePrefix + (i + 1);
                singleThreadRunner.Thread.IsBackground = true;

                threads.Enqueue(singleThreadRunner);
                threadsIdle.Enqueue(singleThreadRunner);

                singleThreadRunner.Thread.Start();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Dequeues the work item.
        /// </summary>
        /// <param name="singleThreadRunner">The single thread runner.</param>
        /// <param name="isGetNewOne">
        ///     if set to <c>true</c> [is get new one].
        /// </param>
        /// <param name="returnedWorkItem">The returned work item.</param>
        /// <returns>
        ///     <see langword="true" />, if a work item has been
        ///     successfully dequeued. <see langword="false" /> otherwise.
        /// </returns>
        internal WorkItem DequeueWorkItemInternal(SingleThreadRunner singleThreadRunner, bool isGetNewOne,
                                                  WorkItem returnedWorkItem = null)
        {
            if (returnedWorkItem != null)
            {
                returnedWorkItems.Enqueue(returnedWorkItem);
            }

            if (!shutDownSignaled && isGetNewOne)
            {
                WorkItem workItem;
                if (workItemQueue.TryDequeue(out workItem))
                {
                    workItem.SingleThreadRunner = singleThreadRunner;
                    return(workItem);
                }
            }

            // If we are here, there is no more work to do left...
            // The worker has to be set to idle...
            Interlocked.Decrement(ref threadsWorking);
            threadsIdle.Enqueue(singleThreadRunner);
            return(null);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Dequeues the work item.
        /// </summary>
        /// <param name="singleThreadRunner">The single thread runner.</param>
        /// <param name="isGetNewOne">
        ///     if set to <c>true</c> [is get new one].
        /// </param>
        /// <param name="returnedWorkItem">The returned work item.</param>
        /// <returns>
        ///     <see langword="true" />, if a work item has been
        ///     successfully dequeued. <see langword="false" /> otherwise.
        /// </returns>
        internal WorkItem DequeueWorkItemInternal(SingleThreadRunner singleThreadRunner, bool isGetNewOne,
            WorkItem returnedWorkItem = null)
        {
            if (returnedWorkItem != null)
            {
                returnedWorkItems.Enqueue(returnedWorkItem);
            }

            if (!shutDownSignaled && isGetNewOne)
            {
                WorkItem workItem;
                if (workItemQueue.TryDequeue(out workItem))
                {
                    workItem.SingleThreadRunner = singleThreadRunner;
                    return workItem;
                }
            }

            // If we are here, there is no more work to do left...
            // The worker has to be set to idle...
            Interlocked.Decrement(ref threadsWorking);
            threadsIdle.Enqueue(singleThreadRunner);
            return null;
        }