I changed the TestSmartThreadPool demo to do this :
for (int i = 0; i < 20; i++)
workItemCallback = new WorkItemCallback(this.DoWork);
Some times, the ThreadPool creates only 19 threads instead of 20. Even if MaxThreads is set to 25.
I suppose the SmartThreadPool follows a scenario like this :
- Initially : queue and ThreadPool are empty.
- Main thread : Enqueue Job #1 ( so WaitingCallbacks is set to 1 and a new thread #1 is created)
- Thread #1 : Dequeue Job #1 ( so WaitingCallbacks is set to 0)
- Main thread : Enqueue Job #2 ( WaitingCallbacks is incremented but InUseThreads is still 0, so no thread is created here )
- Thread #1 : Start Job #1 (InUseThreads is set to 1)
... and so on.
I worked around by adding a Thread.Sleep(10) between each job creation but it's not optimized ...