Work Stealing Queues in .Net 4 and in Previous Versions
.Net 4.0 Beta 1 offers a new and improved thread pool engine. It uses work stealing queues to provide nice load-balancing capabilities, better performance and greater scalability. The work stealing mechanism allows you to reduce the scheduling overhead in highly parallelized algorithms.A well-known proverb says "A problem shared is a problem halved". In my previous post "Lightweight Concurrency: Threads are on a Diet", I began talking about the advantages of following James Reinders' eight key rules for multicore programming.
.Net Framework 4.0 with its Parallel Extensions, still in Beta 1, will add the possibility to work with Tasks. Thus, it will be easy to follow James Reinders' rule #3. However, it will require waiting for .Net Framework 4.0 final version. I've received many e-mails talking about the same situation: C# applications looking forward for task-based programming support in current stable .Net versions. I promised a new post offering a nice solution to this problem this week. I'll begin talking about .Net 4.0 work stealing advantages and then, I'll offer an alternative to implement something similar in .Net 2.0 or greater.
You already know the story. .Net 3.5 threading model has some limitations to create highly parallelized algorithms. The great problem is that it becomes really difficult to achieve great scalability without needing to create very complex code. The number of cores will go on increasing and you need more scalability. Work stealing queues offer a great alternative to reduce the locks and to schedule small work chunks without adding a significant overhead.
Synchronizing code running in multiple threads is indeed complex. Thus, a task-based approach offers an excellent alternative to leave some synchronization problems behind, especially those about work scheduling mechanisms.
.Net Framework 4.0 Beta 1 offers a new thread pool engine that uses a global queue and local queues. It uses work stealing queues (WSQ) to reduce the required locks. Hence, it reduces the scheduling overhead and improves overall performance. Besides, it improves scalability as the number of cores increases. With eight logical cores, you should expect at least eight threads running concurrently and scheduling tasks as necessary. This number should increase in the forthcoming years. Thus, the work stealing queues are appropriate for this new scenario.
Work stealing queues follow James Reinders' rule #5 "Avoid using locks. Simply say "no" to locks. Locks slow programs, reduce their scalability…". Therefore, the new thread pool engine reduces the number of locks and allows multicore developers to achieve greater scalabilities.
The task scheduler relies on the underlying thread pool engine. Thus, when you create a new task, it will use the steal working queues to find the most appropriate thread to enqueue it. The good news is that you'll see performance improvement in your threaded code even without using tasks.
However, there are a few problems. On the one hand, you have to wait for Visual Studio 2010 final version to take advantage of these improvements. On the other hand, you'll be able to use Parallel Extensions in neither C# 3.0 nor Silverlight 3. How can you implement a task-based approach in current C# versions?
Luckily, if you do like to research and you want to learn how things work in the background, there is a nice free implementation of a task-based work stealing threading library in Codeplex, ParallelTasks. It is a beta version, an open source homemade implementation of some of the features found in the Task Parallel Library. However, it is very interesting to read the code and to use it in your projects to implement a task-based approach. There are some issues to solve, but you'll be able to learn very interesting techniques from this project. Besides, you'll be able to use it in current C# versions, in Silverlight applications and in XNA Game Studio, among others.
If you need to code a task-oriented design and you cannot wait for Visual Studio 2010, you can try ParallelTasks and it will help you to find your way to Go Parallel.
Besides, you can find a nice demonstration of the work stealing queues on .Net 4.0's thread pool engine in this post from Daniel Moth.
You can read an in-depth analysis of work stealing queues in this great post from Parallel Extensions' lead developer and architect, Joe Duffy.