Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Gastón Hillar

Dr. Dobb's Bloggers

Work Stealing Queues in .Net 4 and in Previous Versions

June 19, 2009

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

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.