System.Threading.Tasks.ThreadWorker.WorkerMethod C# (CSharp) Method

WorkerMethod() private method

private WorkerMethod ( ) : bool
return bool
		bool WorkerMethod ()
		{		
			bool result = false;
			bool hasStolenFromOther;
			do {
				hasStolenFromOther = false;
				
				Task value;
				
				// We fill up our work deque concurrently with other ThreadWorker
				while (sharedWorkQueue.Count > 0) {
					while (sharedWorkQueue.TryTake (out value)) {
						dDeque.PushBottom (value);
					}
					
					// Now we process our work
					while (dDeque.PopBottom (out value) == PopResult.Succeed) {
						if (value != null) {
							value.Execute (childWorkAdder);
							result = true;
						}
					}
				}
				
				// When we have finished, steal from other worker
				ThreadWorker other;
				
				// Repeat the operation a little so that we can let other things process.
				for (int j = 0; j < maxRetry; j++) {
					// Start stealing with the ThreadWorker at our right to minimize contention
					for (int it = stealingStart; it < stealingStart + workerLength; it++) {
						int i = it % workerLength;
						if ((other = others [i]) == null || other == this)
							continue;
						
						// Maybe make this steal more than one item at a time, see TODO.
						if (other.dDeque.PopTop (out value) == PopResult.Succeed) {
							hasStolenFromOther = true;
							if (value != null) {
								value.Execute (childWorkAdder);
								result = true;
							}
						}
					}
				}
			} while (sharedWorkQueue.Count > 0 || hasStolenFromOther);
			
			return result;
		}
		

Same methods

ThreadWorker::WorkerMethod ( Func predicate, IProducerConsumerCollection sharedWorkQueue, ThreadWorker others ) : void

Usage Example

Example #1
0
 // Called with Task.WaitAll(someTasks) or Task.WaitAny(someTasks) so that we can remove ourselves
 // also when our wait condition is ok
 public void ParticipateUntilInternal(Task self, ManualResetEventSlim evt, int millisecondsTimeout)
 {
     if (millisecondsTimeout == -1)
     {
         millisecondsTimeout = int.MaxValue;
     }
     ThreadWorker.WorkerMethod(self, evt, millisecondsTimeout, workQueue, workers, pulseHandle);
 }
All Usage Examples Of System.Threading.Tasks.ThreadWorker::WorkerMethod