views:

522

answers:

5

I have a basic cs-major understanding of multi-threading but have never had to do anything beyond simple timers in an application. Does anyone know of a good resource that will give me a tour how to work with multi-threaded applications, explaining the basics and maybe posing some of the more difficult stuff?

+24  A: 

This is a great free resource by Joseph Albahari. Threading in C#

Gulzar
+5  A: 

There are 4 basic ways to synchronize threads in .Net:

  • BackgroundWorker control
  • WaitHandles
  • Callback functions
  • polling an ASyncResult object

Generally you want to start at the top of that list and work down. That means first look and see if a backgroundworker control is appropriate to the situation. However, it pretty much assumes windows forms and that you're only spawning one new thread.

So next try waithandles. Waithandles are good for coordinating several threads together. You can kick them all off and wait for them all to finish, or if you want to keep a certain number active you keep waiting for just one and spawning the next when it finishes. Or maybe you know one thread will finish much sooner, so you can wait for it to finish, do a little bit of work, and then wait for the rest to finish.

Waithandles might seem like a bit much if, say, you're only spawning one additional thread and you don't want to block until it's finished. Then you might use a callback, so that the function you designate will be called as soon as the thread completes.

Finally, if and only if for some reason none of the above will work you can fall back to polling.

I can think of 5 different ways to get a new thread in .Net, also roughly in order:

  • OS created, normally as the result of winforms event (including the BackgoundWorker).
  • Obj.Begin___()/End____(). Certain CLR classes already have these asynchronous methods defined for you, and obviously you want to use them when they're available.
  • ThreadPool.QueueUserWorkItem(). Use this most of the time to create your own threads.
  • Delegate.BeginInvoke()/EndInvoke(). You can wrap any method this way.
  • Thread.Start(). You could do it this way, but I read something recently (don't have the link now) that if QueueUserWorkItem won't work the delegate method is probably better.
Joel Coehoorn
+11  A: 

Two great articles:

What Every Dev Must Know About Multithreaded Apps
Understand the Impact of Low-Lock Techniques in Multithreaded Apps

Although this article isn't exactly what you are looking for specifically, it will hopefully be of assistance generally (i.e. it is related, and a very good read):

The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software

Jason Bunting
+2  A: 

A good web-resource to learn about multi-threading in .NET:

Pascal
+1  A: 

One of the best resources I know on the subject is the "threading in C#" book: http://www.albahari.com/threading/

I has a great overview of all a .net developer need to understand in order to program multi threaded applications.

Dror Helper