views:

1261

answers:

11

I used mupltiple threads in a few programs, but still don't feel very comfortable about it.

What multithreading libraries for C#/.NET are out there and which advantages does one have over the other?

By multithreading libraries I mean everything which helps make programming with multiple threads easier.

What .NET integratet (i.e. like ThreadPool) do you use periodic? Which problems did you encounter?

A: 

My advise would be to get comfortable with the thread pool before you move to any other libraries. A lot of the framework code uses the thread pool, so even if you happen to find The Best Threads Library(TM), you will still have to work with the thread pool, so you really need to understand that.

You should also keep in mind that a lot of work has been put into implementing the thread pool and tuning it. The upcoming version of .NET has numerous improvements triggered by the development the parallel libraries.

In my point of view many of the "problems" with the current thread pool can be amended by knowing its strengths and weaknesses.

Brian Rasmussen
"...knowing its strengths and weaknesses." As asked in the question, which problems did you encounter. I still believe one don't have to make a mistake to learn, I try to learn from otherones mistakes, too.
Peter
A: 

For me the builtin classes of the Framework are more than enough. The Threadpool is odd and lame, but you can write your own easily.

I often used the BackgroundWorker class for Frontends, cause it makes life much easier - invoking is done automatically for the eventhandlers.

I regularly start of threads manually and safe them in an dictionary with a ManualResetEvent to be able to examine who of them has ended already. I use the WaitHandle.WaitAll() Method for this. Problem there is, that WaitHandle.WaitAll does not acceppt Arrays with more than 64 WaitHandles at once.

BeowulfOF
+10  A: 

There are various reasons for using multiple threads in an application:

  • UI responsiveness
  • Concurrent operations
  • Parallel speedup

The approach one should choose depends on what you're trying to do. For UI responsiveness, consider using BackgroundWorker, for example.

For concurrent operations (e.g. a server: something that doesn't have to be parallel, but probably does need to be concurrent even on a single-core system), consider using the thread pool or, if the tasks are long-lived and you need a lot of them, consider using one thread per task.

If you have a so-called embarrassingly parallel problem that can be easily divided up into small subproblems, consider using a pool of worker threads (as many threads as CPU cores) that pull tasks from a queue. The Microsoft Task Parallel Library (TPL) may help here. If the job can be easily expressed as a monadic stream computation (i.e. with a query in LINQ with work in transformations and aggregations etc.), Parallel LINQ (same link) which runs on top of TPL may help.

There are other approaches, such as Actor-style parallelism as seen in Erlang, which are harder to implement efficiently in .NET because of the lack of a green threading model or means to implement same, such as CLR-supported continuations.

Barry Kelly
I've used TPL with success in the past--and it's very easy to get started with, as far as threading goes anyway.
Mufasa
+1  A: 

Please keep in mind that you really should be closing threads (or allowing the threadpool to dispose) when you no longer need them, unless you will need them again soon. The reason I say this is that each thread requires stack memory (usually 1mb), so when you have applications sitting on threads but not using them, you are wasting memory.

For exmaple, Outlook on my machine right now has 20 threads open and is using 0% CPU. That is simply a waste of (a least) 20mb of memory. Word is also using another 10 threads with 0% CPU. 30mb may not seem like much, but what if every application was wasting 10-20 threads?

Again, if you need access to a threadpool on a regular basis then you don't need to close it (creating/destroying threads has an overhead).

Richard Szalay
Not wasting memory - wasting virtual address space. Most languages on Windows reserve 1MB of stack space upon thread creation, yes, but it is committed on demand using special page protection (a guard page).
Barry Kelly
Memory not committed until needed, true, but is it cleaned up when it's no longer needed? My issue is not with creating threads and not using them, it's using them for a short term task and not disposing them when they are no longer needed.
Richard Szalay
+1  A: 

Check out the Power Threading library.

HTH, Kent

Kent Boogaart
The power threading lib is very cool, but i wouldn't use some of it's features in production code as it takes advantage of a few compiler patterns that are not guaranteed to be maintained in future releases of .net
Brian Rudolph
+2  A: 

I have a pretty good blog on PLINQ and why it makes it easy for programmers who haven't done too much multi-threaded programming to actually write some decent threaded apps here...

Enjoy!

matt_dev
+1  A: 

You don't have to use the threadpool explicitly, you can use BeginInvoke-EndInvoke if you need async calls. It uses the threadpool behind the scenes. See here: http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

tuinstoel
+2  A: 

I have written alot of threading code in my days... even implemented my own threading pool & dispatcher. Alot of it is documented here.

http://devplanet.com/blogs/brianr/default.aspx

Just realize that i wrote these for very specific purposes and tested them in those conditions, and there is no real silver-bullet.

Brian Rudolph
+1  A: 

You should take a look at the Concurrency & Coordination Runtime. The CCR can be a little daunting at first as it requires a slightly different mind set. This video has a fairly good job of explanation of its workings...

In my opinion this would be the way to go, and I also hear that it will use the same scheduler as the TPL.

Matt Davison
+1  A: 

I like this one http://www.codeplex.com/smartthreadpool

Simon
A: 

Hi! You might want to look at the series of articles about threading patterns. Right now it has sample codes for implementing a WorkerThread and a ThreadedQueue.

http://devpinoy.org/blogs/jakelite/archive/tags/Threading+Patterns/default.aspx

jake.stateresa