last day (14 days later) » 

1:06 PM
1
A: understanding Parallel.Invoke, creation and reusing of threads

EvkIf you pass less then 10 items to Parallel.Invoke, and you don't specify MaxDegreeOfParallelism in options (so - your case), it will just run them all in parallel on thread pool sheduler using rougly the following code: var actions = new [] { action, action, action, action, action, action, actio...

 
You say the only reason that threads are reused is that some actions are finished before the scheduler has started all actions. I modified the code such that this is not the case any more: I put long i = 100000000; while (i > 0) { i--; } between the bool repeat = ...; and Console.WriteLine(...); lines. This makes each action take around 2-3 seconds. Still, only 9 different threads are used instead of 10.
 
Evk
@Kjara I've updated answer to answer your question.
 
To measure the time, I had this code inside the action curly brackets: Stopwatch sw = new Stopwatch(); sw.Start(); bool repeat = ThreadName.IsValueCreated; long i = 100000000; while (i > 0) { i--; } Console.WriteLine("ThreadName = {0} {1}" , ThreadName.Value, (repeat ? "(repeat)" : "")); Console.WriteLine(sw.Elapsed); It gave me outputs between 2 and 3 seconds.
 
Evk
@Kjara ensure then that ALL of them run between 2-3 seconds. Because if even one runs in less than thread pool thread creation delay - it will be free for new work. But anyway, just introduce big delay with Thread.Sleep(1000000) and run 100 actions in parallel to verify my point. No need to test on edge cases.
 
But if I change the start value for i to 500000000 (results in around 14 seconds for each action), I can see that new threads are created and none are reused. Maybe the delay before creating new threads is higher on my PC. How can I retrieve that delay time?
 
Evk
1:06 PM
As far as I know, this is not configurable but might be different in different .NET framework versions. I use .NET 4.5.2 for those tests, and which one you use?
 
I use 4.5.2 as well. Maybe there is a huge difference between the real execution time and the time measured by StopWatch...
 
Evk
Maybe you misinterpet it somehow?
What ElapsedMilliseconds show?
 
25xx to 29xx, so still 2-3 seconds ;)
But anyway, I learned a lot from your answer. Thanks!
 
Evk
ok, you are welcome :)
by the way, what ThreadPool.GetMinThreads show?
 
both return values are 8
 
Evk
1:22 PM
Probably delay is longer because all cpus are busy with that calculation
If I replace that with Thread.Sleep - I don't see that behavior
and thread creation delay seems to be exactly 1 second
also note that Parallel.Invoke reuses your current thread also
so it runs 1 action on current thread and 8 actions on thread pool threads
seems to be indeed related to doing cpu-intensive work
maybe thread pool has more heuristics internally
like if he sees that there are 8 threads already and they all are busy with CPU-intensive work - no reason to create more
but that's just wild guessing of course
 

last day (14 days later) »