Skip to content

Commit

Permalink
Add a SpinWait step when waiting on a task. Fix 100% CPU bother.
Browse files Browse the repository at this point in the history
After some time the call to SpinWait.SpinOnce will fall back to the more stalling Thread.Sleep (1) call.
  • Loading branch information
garuma committed Jul 29, 2010
1 parent cba2d8a commit 99e736d
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions mcs/class/corlib/System.Threading.Tasks/Internal/ThreadWorker.cs
Expand Up @@ -212,6 +212,8 @@ bool WorkerMethod ()
public static void WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
ThreadWorker[] others)
{
SpinWait wait = new SpinWait ();

while (!predicate ()) {
Task value;

Expand All @@ -226,9 +228,8 @@ bool WorkerMethod ()
}

// 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;
Expand All @@ -245,10 +246,11 @@ bool WorkerMethod ()
}
}

if (predicate ()) {
if (predicate ())
return;
}
}

wait.SpinOnce ();
}
}

Expand Down

0 comments on commit 99e736d

Please sign in to comment.