What kinds of applications need to be multi-threaded


What are some concrete examples of applications that need to be multi-threaded, or don't need to be, but are much better that way?

Answers would be best if in the form of one application per post that way the most applicable will float to the top.

Best Solution

There is no hard and fast answer, but most of the time you will not see any advantage for systems where the workflow/calculation is sequential. If however the problem can be broken down into tasks that can be run in parallel (or the problem itself is massively parallel [as some mathematics or analytical problems are]), you can see large improvements.

If your target hardware is single processor/core, you're unlikely to see any improvement with multi-threaded solutions (as there is only one thread at a time run anyway!)

Writing multi-threaded code is often harder as you may have to invest time in creating thread management logic.

Some examples

  • Image processing can often be done in parallel (e.g. split the image into 4 and do the work in 1/4 of the time) but it depends upon the algorithm being run to see if that makes sense.
  • Rendering of animation (from 3DMax,etc.) is massively parallel as each frame can be rendered independently to others -- meaning that 10's or 100's of computers can be chained together to help out.
  • GUI programming often helps to have at least two threads when doing something slow, e.g. processing large number of files - this allows the interface to remain responsive whilst the worker does the hard work (in C# the BackgroundWorker is an example of this)

GUI's are an interesting area as the "responsiveness" of the interface can be maintained without multi-threading if the worker algorithm keeps the main GUI "alive" by giving it time, in Windows API terms (before .NET, etc) this could be achieved by a primitive loop and no need for threading:

MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))

    // do some stuff here and then release, the loop will come back
    // almost immediately (unless the user has quit)