Search code examples
c#multithreadingtask-parallel-librarythreadpoolparallel.foreach

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";


Solution

  • 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.