Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some items of Parallel.ForEach<T> run on ThreadPool, some don't

I have a simple algorithm like this:

Parallel.ForEach(myList, new ParallelOptions() { MaxDegreeOfParallelism = 4 } ,(job) => job.doSomething());  

myList is a List<MyType>.

MyType has void DoSomething().

inside DoSomething I check Thread.CurrentThread.IsThreadPoolThread. Some threads aren't "ThreadPooled";

like image 607
Luiz Marques Avatar asked Dec 11 '25 00:12

Luiz Marques


1 Answers

The functions defined in Parallel will also use the thread you called the function from as one of the worker threads. Most likely the non Thread Pool threads where jobs done on the thread you called Parallel.ForEach from.

like image 103
Scott Chamberlain Avatar answered Dec 12 '25 14:12

Scott Chamberlain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!