views:

157

answers:

5

As far as I'm concerned, the ideal amount of threads is 3: one for the UI, one for CPU resources, and one for IO resources.

But I'm probably wrong.

I'm just getting introduced to them, but I've always used one for the UI and one for everything else.

When should I use threads and how? How do I know if I should be using them?

+2  A: 

From the SQLite FAQ: "Threads are evil. Avoid Them." Only use them when you absolutely have to.

If you have to, then take steps to avoid the usual carnage. Use thread pools to execute fine-grained tasks with no interdependencies, using GUI-framework-provided facilities to dispatch outcomes back to the UI. Avoid sharing data between long-running threads; use message queues to pass information between them (and to synchronise).

A more exotic solution is to use languages such as Erlang that are explicit designed for fine-grained parallelism without sacrificing safety and comprehensibility. Concurrency itself is of fundamental importance to the future of computation; threads are simply a horrible, broken way to express it.

Marcelo Cantos
don't be dogmatic. Threads are not evil by themselves. It is great pattern to separate UI and background activity thread, it provides better user experience.
Andrey
+1: if you can get away with not using threads then do so - it will certainly make testing and debugging a lot easier, and probably make for a more stable product in the long run. If you *have* to use threads then do it judiciously.
Paul R
@Andrey, the smartest people I know are the least sanguine about using threads in their code. Follow the PDF link I provided.
Marcelo Cantos
@Marcelo Cantos - thx, i read this pdf three times before. what do you mean do not use threads? i have long running IO operation. what do you suggest? wait for it and block everything else? it is stupid, because thread is in waiting state and does nothing useful. another example: you have multicore proc and do CPU intensive operation. suggest to use only one core because threads are evil?
Andrey
@Andrey, I never said, "do not use threads".
Marcelo Cantos
@Marcelos, the PDF isn't saying not to use parallelism, just not to use the threading idiom to accomplish it. You might want to make it clear that you are simply advocating parallelism via message passing between sequential processes rather than advocating no parallelism.
Michael Aaron Safyan
+6  A: 

The answer totally depends on what you're planning on doing. However, one for CPU resources is a bad move - your CPU may have up to six cores, plus hyperthreading, in a retail CPU, and most CPUs will have two or more. In this case, you should have as many threads as CPU cores, plus a few more for scheduling mishaps. The whole CPU is not a single-threaded beast, it may have many cores and need many threads for 100% utilization.

You should use threads if and only if your target demographic will virtually all have multi-core (as is the case in current desktop/laptop markets), and you have determined that one core is not enough performance.

DeadMG
+5  A: 

Unfortunately, there are no hard and fast rules to using Threads. If you have too many threads the processor will spend all its time generating and switching between them. Use too few threads you will not get the throughput you want in your application. Additionally using threads is not easy. A language like C# makes it easier on you because you have tools like ThreadPool.QueueUserWorkItem. This allows the system to manage thread creation and destruction. This helps mitigate the overhead of creating a new thread to pass the work onto. You have to remember that the creation of a thread is not an operation that you get for "free." There are costs associated with starting a thread so that should always be taken into consideration.

Depending upon the language you are using to write your application you will dictate how much you need to worry about using threads.

The times I find most often that I need to consider creating threads explicitly are:

  • Asynchronous operations
  • Operations that can be parallelized
  • Continual running background operations
Craig Suchanec
+1  A: 

The "ideal number of threads" depends on your particular problem and how much parallelism you can exploit. If you have a problem that is "embarassingly parallel" in that it can be subdivided into independent problems with little to no communication between them required, and you have enough cores that you can actually get true parallelism, then how many threads you use depends on things like the problem size, the cache line size, the context switching and spawning overhead, and various other things that is really hard to compute before hand. For such situations, you really have to do some profiling in order to choose an optimal sharding/partitioning of your problem across threads. It typically doesn't make sense, though, to use more threads than you do cores. It is also true that if you have lots of synchronization, then you may, in fact, have a performance penalty for using threads. It's highly dependent on the particular problem as well as how interdependent the various steps are. As a guiding principle, you need to be aware that spawning threads and thread synchronization are expensive operations, but performing computations in parallel can increase throughput if communication and other forms of synchronization is minimal. You should also be aware that threading can lead to very poor cache performance if your threads end up invalidating a mutually shared cache line.

Michael Aaron Safyan
+1  A: 

Herb Sutter wrote an article for Dr. Dobb's Journal in which he talks about the three pillars of concurrency. This article does a very good job of breaking down which problems are good candidates for being solved via threading constructs.

Jesse C. Slicer