.NET Tips and Tricks

Blog archive

Upgrade Threaded Applications to .NET 4... and then Rewrite

Prior to .NET Framework 4, threading used a locking mechanism that added to the thread overhead. In .NET 4 the locking mechanism is eliminated. On top of that, threading integrates better with garbage collection, reducing the time required to clean up after a thread finished running

Putting those two changes together means that just moving your multi-threaded application to .NET 4 can improve its performance. In addition, there was a bug in prior versions of .NET around aborting threads that issued locks. If you aborted a thread at the wrong time (when a no-operation instruction that was part of the generated IL code was being processed), the lock might never be released. If you've been trying to figure out why your multi-threaded application's performance degrades over time, it's a sign that the upgrade to .NET 4 might not be an optional activity.

If you've got time, after you upgrade, you should consider rewriting your code to use the Task Parallel Library (TPL). In previous versions of .NET 4, while you could queue up threads, you couldn't provide much information to .NET about which threads were most important. In the absence of that information, .NET treated all queued threads equally.

The TPL allows you to provide more information to .NET's scheduler to improve how your threads are handled. All by itself, the TPL gives you the ability to sequence tasks by having one task kick off as soon as another completes. See my VSM feature article on PLINQ and the TPL for more information.

Posted by Peter Vogel on 05/17/2011


comments powered by Disqus

Featured

Subscribe on YouTube