Beispiel #1
0
        /// <summary>
        /// Constructor of DiReCT threadpool
        /// </summary>
        /// <param name="SizeMax">
        ///     Maximum threadpool size
        /// </param>
        /// <param name="SizeMin">
        ///     Minimum threadpool size
        /// </param>
        /// <param name="ThreadIdleTime">
        /// The number of seconds a thread must be idle before decreasing
        /// threadpool size to SizeMin
        /// </param>
        public DiReCTThreadPool(int SizeMax, int SizeMin = 5,
                                int IdleTime             = 60)
        {
            //check if variables are proper
            if (SizeMax < SizeMin)
            {
                throw new ArgumentException(
                          "Parameter can not be less than MinThread", "MaxThread");
            }
            if (SizeMin < 1)
            {
                throw new ArgumentException(
                          "Parameter can not be less than 1", "MinThread");
            }
            if (IdleTime < 0)
            {
                throw new ArgumentException(
                          "Parameter can not be negative", "IdleTime");
            }

            // Create a thread pool controller
            threadPoolController = new ThreadpoolController(SizeMax,
                                                            SizeMin,
                                                            IdleTime,
                                                            ThreadWork);
            threadPoolController.Init();

            // Ask for a ThreadItem and assign it as a leader
            leaderThreadItem = threadPoolController.GetThreadItem();
            leaderThreadItem.PromoteNewLeaderEvent.Set();
        }
Beispiel #2
0
 public void Init()
 {
     for (int i = 0; i < minThread; i++)
     {
         // All thread join idleThreadQueue
         ThreadItem threadItem = CreateThread();
         IdleThreadQueue.Enqueue(threadItem);
         Debug.WriteLine("Thread[{0}] Create Done",
                         threadItem._Thread.ManagedThreadId);
     }
 }
Beispiel #3
0
 private void DebugGetLeaderThread(ThreadItem threadItem)
 {
     if (threadItem != null)
     {
         Debug.WriteLine("Thread[{0}] is Leader Create Done",
                         threadItem._Thread.ManagedThreadId);
     }
     else
     {
         Debug.WriteLine("No idle threads");
     }
 }
Beispiel #4
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            threadPoolController.Dispose();

            if (disposing)
            {
                leaderThreadItem = null;
                workQueue        = null;
                workQueueLock    = null;
            }

            disposed = true;
        }
Beispiel #5
0
        private ThreadItem CreateThread()
        {
            ThreadItem threadItem;

            lock (CurrentThreadQuantityLock)
                if (CurrentThreadQuantity < maxThread)
                {
                    threadItem = new ThreadItem(threadWork);
                    ThreadItemList.Add(threadItem);
                    threadItem._Thread.Start(threadItem);
                    CurrentThreadQuantity++;
                }
                else
                {
                    threadItem = null;
                }
            return(threadItem);
        }
Beispiel #6
0
        private void ThreadWork(object ThreadItem)
        {
            ThreadItem myThreadItem = (ThreadItem)ThreadItem;

            // Check whether the thread pool/follower thread is terminating
            while (threadPoolController.IsRunning && myThreadItem.IsRunning)
            {
                Debug.WriteLine("Thread[{0}] Waiting to be a Leader",
                                (myThreadItem)._Thread.ManagedThreadId);

                myThreadItem.PromoteNewLeaderEvent.WaitOne();

                // Record the time when thread becomes a leader
                threadPoolController.BecomeLeaderTimePeriod = DateTime.Now;

                //Wait for work to arrive
                SpinWait.SpinUntil(() => (
                                       workQueue.Count != 0 ||
                                       !threadPoolController.IsRunning ||
                                       !(myThreadItem.IsRunning)
                                       ));

                if (threadPoolController.IsRunning &&
                    (myThreadItem).IsRunning)
                {
                    // Get work from WorkQueue
                    WorkItem Work = null;

                    if (workQueue.Count != 0)
                    {
                        lock (workQueueLock)
                            Work = workQueue.Dequeue();
                    }

                    if (Work != null)
                    {
                        // Ask for a thread and assign it as a leader
                        lock (threadPoolController.ThreadVariableChangeLock)
                            leaderThreadItem = threadPoolController.
                                               GetThreadItem();
                        if (leaderThreadItem != null)
                        {
                            leaderThreadItem.PromoteNewLeaderEvent.Set();
                        }


                        // Do work
                        try
                        {
                            switch (Work.GroupName)
                            {
                            //All DM Function
                            case FunctionGroupName.DataManagementFunction:

                                DMModule.DMWorkerFunctionProcessor(Work);
                                break;

                            case FunctionGroupName.QualityControlFunction:
                                RTQCModule.RTQCWorkerFunctionProcessor(
                                    Work);

                                break;
                            }
                        }
                        catch
                        {
                            // Reset after the thread is aborted
                            if (Thread.CurrentThread.ThreadState
                                == System.Threading.ThreadState.AbortRequested ||
                                Thread.CurrentThread.ThreadState.ToString()
                                == "Background, AbortRequested")
                            {
                                Debug.WriteLine(
                                    "Thread[{0}] is aborted,reset Thread[{0}]",
                                    Thread.CurrentThread.ManagedThreadId);
                                Thread.ResetAbort();
                            }
                        }
                    }
                    // Become leader if no leader, else join idle thread queue
                    lock (threadPoolController.ThreadVariableChangeLock)
                        if (leaderThreadItem == null)
                        {
                            leaderThreadItem = myThreadItem;
                            leaderThreadItem.PromoteNewLeaderEvent.Set();
                            Debug.WriteLine("Thread[{0}] become a new leader",
                                            Thread.CurrentThread.ManagedThreadId);
                        }
                        else
                        {
                            Debug.WriteLine("Thread[{0}] join idle queue",
                                            Thread.CurrentThread.ManagedThreadId);
                            threadPoolController.EnqueueIdleThreadQueue
                                = myThreadItem;
                        }
                }
            }

            // Release thread when finish
            Debug.WriteLine("Thread[{0}] Close Done",
                            (myThreadItem)._Thread.ManagedThreadId);
            threadPoolController.ReduceCurrentThreadQuantity();
            threadPoolController.RemoveThread(myThreadItem);
            (myThreadItem).PromoteNewLeaderEvent.Close();
            (myThreadItem)._Thread = null;
        }
Beispiel #7
0
 // Removes the specified Threaditem from ThreadItemList
 public void RemoveThread(ThreadItem threaditem)
 {
     ThreadItemList.Remove(threaditem);
 }