After some time the call to SpinWait.SpinOnce will fall back to the more stalling Thread.Sleep (1) call.
public static void WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
ThreadWorker[] others)
{
+ SpinWait wait = new SpinWait ();
+
while (!predicate ()) {
Task value;
}
// First check to see if we comply to predicate
- if (predicate ()) {
+ if (predicate ())
return;
- }
// Try to complete other work by stealing since our desired tasks may be in other worker
ThreadWorker other;
}
}
- if (predicate ()) {
+ if (predicate ())
return;
- }
}
+
+ wait.SpinOnce ();
}
}