Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created September 6, 2012 21:39
Show Gist options
  • Save peteraritchie/3660551 to your computer and use it in GitHub Desktop.
Save peteraritchie/3660551 to your computer and use it in GitHub Desktop.
show TPL using more threads than CPUs
int threadCount = Environment.ProcessorCount + 5;
Parallel.ForEach(Enumerable.Range(0, threadCount),
new ParallelOptions { MaxDegreeOfParallelism = -1 }, item =>
{
Interlocked.Increment(ref level);
Trace.WriteLine(string.Format("in {0} {1}", item,
level));
Stopwatch sw = Stopwatch.StartNew();
while ((sw.Elapsed.TotalSeconds < TimeSpan.FromSeconds(Environment.ProcessorCount * 25).TotalSeconds))
{
}
Trace.WriteLine(string.Format(
"leaving {0} {1}", item, level));
Interlocked.Decrement(ref level);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment