Lately I’ve been doing a lot of multithreaded programming and have been searching for different ways to accomplish this task. It’s certainly not straightforward and requires a solid understanding of the CLR and how .NET works under the hood to get started. This post is more of a "catch all" of references to various methods to approach multithreading in .NET, mainly so I can find things later
It shouldn’t come as any surprise that the basic building block of multithreaded applications is the System.Threading.Thread class. It’s relatively easy to get started using threads this way if you stick to scenarios where you need to pass in 0 or more input parameters and expect no output parameters. More advanced communication between threads can get messy, but doing threading this way gives you access to all of the low-level stuff if you know what you’re doing (or feel lucky). For a nice in-depth tutorial, I highly recommend checking out Joseph Alibari’s free ebook "Threading in C#". It’s even available as a single PDF.
In the .NET Framework 2.0, it was noticed that creating your own threads can be cumbersome and the idea of a common Thread Pool was introduced. One particularly useful helper method is the QueueUserWorkItem that lets you enqueue a delegate to a method that completes a work task so that it can be executed on another thread. This also works great if you need to pass in 0 or more input parameters and expect nothing to be returned when the thread is completed. In addition, only a few small modifications are needed to use anonymous types with the QueueUserWorkItem method.
A common scenario usually encountered when creating responsive user interfaces (e.g. WinForms applications) is to let a long-running piece of code execute on a background thread and then update the user interface when it completes. The catch is that the user interface update at the end must be completed on the UI thread. Thankfully .NET Framework shipped with a BackgroundWorker created explicitly for this task which handles the marshalling between background thread and UI thread automatically. It even supports cancellation and can report progress back to the UI thread in addition to notifying the UI thread that it has completed. This is also discussed in the "Threading in C#" book mentioned above. In addition, only slight modifications to the BackgroundWorker are needed to use anonymous types as well.
There has recently been a lot of excitement surrounding the Parallel Extensions for the .NET Framework. The group just released a June CTP which includes the Task Parallel Library, PLINQ (Parallel LINQ), and Coordination Data Structures. This is great work, especially with the current shift to multi-core processors. Here’s the direct download link if you want to check it out, and there are some impressive (but short) demos with Stephen Toub here and here.
Currency and Coordination Runtime
I just recently stumbled on the Currency and Coordination Runtime (CCR) and am completely blown away. Why haven’t I heard about this before?? Turns out that the CCR is actually a foundational component of the Microsoft Robotics Developer Studio. From the MSDN documentation for the CCR:
The CCR addresses the need of service-oriented applications to manage asynchronous operations, deal with concurrency, exploit parallel hardware and deal with partial failure. It enables you to design your application so that its software modules or components can be loosely coupled; that is, so that they can be developed independently and make minimal assumptions about their runtime environment and other components.
In essence, the CCR is a generic (problem-agnostic) library that doesn’t have anything to do with robotics. Robotics applications, though, are asynchronous, concurrent, parallel programs and are an excellent fit to demonstrate the awesome power and versatility of the CCR.
Just recently there has been a lot of discussion about using the CCR and its sister library DSS (Decentralized Software Services) for enterprise applications and other distributed systems such as MySpace. I just watched this video about the CCR and really want to dig in. George Chrysanthakopoulos is incredibly bright and very passionate about his work with the CCR and robotics studio. Jeffrey Richter has written an excellent article for MSDN Magazine giving a deep-dive into using the CCR for non-robotics applications, in this case asynchronous I/O.
If the rest of the book is as good as the excellent sample chapter about the CCR (PDF and sample code available), then I definitely need to go pick up the recently-released Professional Microsoft Robotics Developer Studio book and dive in.
Just as a small word of caution: The Robotics Studio (including the CCR and DSS) are licensed in such a way that they are free for academic and non-commercial uses, but have a small fee for commercial applications. This is different than the licenses for the .NET Framework and the Parallel Extensions.
I’d seen rumors of the CCR being merged into the Parallel Extensions framework in the future, which would be nice and certainly get more exposure since it is not hidden in the Robotics Studio download (a hefty 400MB for a few sub-megabyte DLLs). As pointed out in a comment by Alpa Agarwal in the above-mentioned blog, the CCR and TPL are complementary:
The Concurrency and Coordination Runtime (CCR) and the Task Parallel Library (TPL) are complementary technologies. TPL, which provides support for imperative data and task parallelism, is well suited for synchronous parallelism and patterns such as parallel loops. The Concurrency and Coordination Runtime is well suited for orchestrating many asynchronous components and handling asynchronous I/O in a clever manner.
Are there any other approaches to multithreading and concurrency in .NET that I have missed? As I get time to play around with these things I’ll try to post examples and samples.
Hopefully someone finds this useful!