For test purposes, I write a CPU voltage program: it just makes N for loops in threads M. I run this program with a lot of threads, say 200. But in the task manager, I see that the thread counter does not exceed some small value, for example 9, and Thread.Start methods wait for the completion of previous running threads.
This behavior is similar to the behavior of ThreadPool, but I expect that regular System.Threading.Thread
should start anyway, without waiting for reasons.
The code below will reproduce this problem and have a workaround option:
using System;
using System.Diagnostics;
using System.Threading;
namespace HeavyLoad
{
class Program
{
static long s_loopsPerThread;
static ManualResetEvent s_startFlag;
static void Main(string[] args)
{
long totalLoops = (long)5e10;
int threadsCount = 200;
s_loopsPerThread = totalLoops / threadsCount;
Thread[] threads = new Thread[threadsCount];
var watch = Stopwatch.StartNew();
for (int i = 0; i < threadsCount; i++)
{
Thread t = new Thread(IntensiveWork);
t.IsBackground = true;
threads[i] = t;
}
watch.Stop();
Console.WriteLine("Creating took {0} ms", watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
foreach (var thread in threads)
{
thread.Start();
}
watch.Stop();
Console.WriteLine("Starting took {0} ms", watch.ElapsedMilliseconds);
if (s_startFlag != null)
s_startFlag.Set();
watch = Stopwatch.StartNew();
foreach (var thread in threads)
{
thread.Join();
}
watch.Stop();
Console.WriteLine("Waiting took {0} ms", watch.ElapsedMilliseconds);
Console.ReadLine();
}
private static void IntensiveWork()
{
if (s_startFlag != null)
s_startFlag.WaitOne();
for (long i = 0; i < s_loopsPerThread; i++)
{
}
}
}
}
1: s_startFlag, . concurrency ( 9 ) :
Creating took 0 ms
Starting took 4891 ms
Waiting took 63 ms
2: s_startFlag, , . 200 : 200 +:
Creating took 0 ms
Starting took 27 ms
Waiting took 4733 ms
? ?
:
- : Windows 7 Professional
- Framework: NET 4.6
- : Intel Core2 Quad Q9550 @2.83
- : 8