Exemple #1
0
        /// <summary>
        /// The thread Processing loop, that consumes up the queue.
        /// </summary>
        private void Process()
        {
            IComputation localComputation = null;

            Thread.BeginThreadAffinity();
            if (cpuId != -1)
            {
                Unsafe.SetThreadAffinityMask(Unsafe.GetCurrentThread(), new IntPtr(1 << (int)cpuId));
            }

            while (true)
            {
                //check if our thread is in the signalled state,
                //if that's true then reset it and continue work.
                if (isSignalled)
                {
                    isSignalled = wait.Reset();
                }

                if (scheduler.IsEmpty == false)
                {
                    localComputation = scheduler.Dequeue();

                    if (localComputation != null)
                    {
                        InvokeAction(localComputation);
                        SmartThreadPool.Reschedule(isInitializedInPool, this, SchedulerAction.Dequeue);
                    }
                }
                else
                {
                    //lets try to steal some of this work.
                    bool workStolen = TryStealWork();

                    //if we stolen something then don't sleap and first check you work queue
                    //and then try to steal again.
                    while (workStolen)
                    {
                        workStolen = TryStealWork();
                    }

                    if (isPendingJoin)
                    {
                        break;
                    }

                    SpinWait();
                }
            }
            //end processing.
            IsStarted = false;
            wait.Close();
            Thread.EndThreadAffinity();
        }