Parallel.ForEach<TSource>
, when TSource
is an IEnumerable<T>
, creates a partitioner for the IEnumerable<T>
that includes its own internal locking mechanism, so you don't need to implement any thread-safety in your iterator.
Whenever a worker thread requests a chunk of items, the partitioner will create an internal enumerator, which:
- acquires a shared lock
- iterates through the source (from where it was left of) to retrieve the chunk of items, saving the items in an private array
- releases the lock so that other chunk requests can be fulfilled.
- serves the worker thread from its private array.
As you see, the run through the IEnumerable<T>
for the purposes of partitioning is sequential (accessed via a shared lock), and the partitions are processed in parallel.