コード例 #1
0
        private bool TryRequestItem(DispatchThread thread, out int priority, out Action callback)
        {
            // check if we can find a work-item in the shared queue
            if (_inbox.TryDequeue(out priority, out callback))
            {
                return(true);
            }

            // try to steal a work-item from another thread; take a snapshot of all allocated threads (needed in case the array is copied for resizing)
            DispatchThread[] threads = _activeThreads;
            foreach (DispatchThread entry in threads)
            {
                // check if we can steal a work-item from this thread
                if ((entry != null) && !ReferenceEquals(entry, thread) && entry.TryStealWorkItem(out callback))
                {
                    priority = ((PrioritizedThreadPool)entry.DispatchQueue).Priority;
                    return(true);
                }
            }

            // check again if we can find a work-item in the shared queue since trying to steal may have overlapped with the arrival of a new item
            if (_inbox.TryDequeue(out priority, out callback))
            {
                return(true);
            }
            return(false);
        }