This article is intended for .NET 2 or 3.5. If you are on .NET 4, use System.Threading.Parallel class.
I was thinking about how Parallel.For could be implemented. I wrote my own, I am using it in my own project and it scales very well.
Here it is:
// locker object shared by all the process() delegates
var locker = new object();
// so the real order of items will be out-of-order)
As noted in the code, by setting chunkSize to 1 we can make the items be processed in order. With bigger chunk size, items can be processed in mixed non-deterministic order, which is ok in some application, like when you want to modify all items of a collection or render lines of a picture.
Bigger chunkSize should reduce lock waiting time and thus increase overall speed. But too big chunkSize is bad too, because if the work is split into only a few big parts, there is not enough parallelism exposed - we could find ourselves waiting for the last single thread to finish its large chunk.
Using Parallel.For is simple: