32

I'm learning Multi Threading at the moment, in C#, but as with all learning I like to learn best practices. At the moment the area seems fuzzy. I understand the basics, and I can create threads.

What should I look out for when creating multi threaded applications. Are there any set rules or best practices that I should know about? Or anything to remember in order to avoid slip ups down the line?

Thanks for the responses.

Kevin Kibler
  • 13,357
  • 8
  • 38
  • 61
James Jeffery
  • 12,093
  • 19
  • 74
  • 108
  • 1
    If you're using C# then you should consider using the Task Parallel Library. It allows you to think about Tasks and Barriers rather than threads and locks. It improves the abstraction. – Ade Miller Jan 28 '10 at 18:37
  • @Ade Miller: This is great advice - I'll add that you can use the TPL and PLINQ in .Net3.5, too, by installing the Reactive Extensions: http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx – Reed Copsey Jan 28 '10 at 18:39

5 Answers5

33

In addition to the MSDN Best Practices, I'll add:

  1. Don't make your own threads. Prefer to use the ThreadPool (or the new Task Parallel Library Tasks). Managing your own thread is rarely, if ever, the correct design decision.
  2. Take extra care with UI related issues. Control.Invoke (Windows Forms) and Dispatcher.Invoke (WPF), or use SynchronizationContext.Current with Post/Send
  3. Favor using the BackgroundWorker class when appropriate.
  4. Try to keep synchronization via locks to a minimum
  5. Make sure to synchronize everything that requires synchronization
  6. Favor the methods in the Interlocked class when possible over locking

Once you get more advanced, and are trying to optimize, other things to look for:

  1. Watch out for false sharing. This is especially problematic when working with arrays, since every array write to any element in an array includes a bounds check in .NET, which in effect causes an access on the array near element 0 (just prior to element 0 in memory). This can cause perf. to go downhill dramatically.
  2. Beware of closure issues, especially when working in looping situations. Nasty bugs can occur if you're closing on a variable in the wrong scope when making a delegate.
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
9

MSDN - Managed Threading Best Practices

That MSDN Article does a really good job at touching on the danger areas and giving the best practices for managing/working around those areas.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
5

Since I have this open on another tab...

http://www.yoda.arachsys.com/csharp/threads/

I'm only a couple chapters in but it was written by Jon Skeet.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
2

Are there things you should know about ? Certainly.

Dangers:

A usefull and interesting article can be found here

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
2

I highly recommend that you start by digesting this: http://www.albahari.com/threading/.

Ed Power
  • 8,310
  • 3
  • 36
  • 42