Skip to content

Instantly share code, notes, and snippets.

@patricksuo
Created March 31, 2017 01:41
Show Gist options
  • Save patricksuo/ecbf780bf24f422f6ab9787c630139cc to your computer and use it in GitHub Desktop.
Save patricksuo/ecbf780bf24f422f6ab9787c630139cc to your computer and use it in GitHub Desktop.
using System;
namespace threadpool_bench
{
class Program
{
static int totalRound;
static System.Threading.ManualResetEventSlim finishEvent;
static System.Threading.WaitCallback job = obj =>
{
var old = System.Threading.Interlocked.Decrement(ref totalRound);
if (old > 0)
{
System.Threading.ThreadPool.QueueUserWorkItem(job);
}
else if (old == 0)
{
finishEvent.Set();
}
};
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
for (var i = 0; i < 10; i++)
{
System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
finishEvent = new System.Threading.ManualResetEventSlim();
watcher.Start();
totalRound = 100 * 100000;
for (var j = 0; j < 1000; j++)
{
System.Threading.ThreadPool.QueueUserWorkItem(job);
}
finishEvent.Wait();
watcher.Stop();
Console.WriteLine("round {0} time {1}", i, watcher.ElapsedMilliseconds);
finishEvent.Wait(500);
}
}
}
}
@patricksuo
Copy link
Author

threadpool domains_lock 0xb389b8, contended 2652436 times, 16 avg us
threadpool_worker parked_threads_lock 0xb38a40, contended 775650 times, 1446 avg us
threadpool_worker parked_threads_cond 0xb38a70, contended 19818 times, 30 avg us
threadpool_worker worker_creation_lock 0xb38ab0, contended 115147 times, 25 avg us

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment