Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 287
Alex Baranosky Avatar asked Jan 18 '09 12:01

Alex Baranosky


People also ask

What kind of applications use multithreading?

For example, a desktop application providing functionality like editing, printing, etc. is a multithreaded application. In this application, as printing is a background process, we can perform editing documents and printing documents concurrently by assigning these functions to two different threads.

When should we use multi threading?

Multithreading is a process of executing multiple threads simultaneously. You should use multithreading when you can perform multiple operations together so that it can save time.


1 Answers

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)) {     TranslateMessage(&msg);     DispatchMessage(&msg);      // do some stuff here and then release, the loop will come back     // almost immediately (unless the user has quit) } 
like image 57
Ray Hayes Avatar answered Oct 12 '22 10:10

Ray Hayes