Adi Levin's Blog for programmers

May 30, 2009

Why do we have to use multithreading?

Multithreading exists for a long while now, long before I ever thought about it. But only in recent years it became an essential part of the knowledge that, I believe, every programmer should have. In particular, for people like me, who make a living writing complicated computational algorithms, multithreading is a basic tool.


Back in the good-old days, we were counting on CPUs to double their performance every year. We knew that the code we write today, will execute much faster in the future. This is no longer the case. CPUs can be made to work faster, but they will consume a lot of power. The issue of power and cooling is important for many configurations – from notebooks to servers. Intel, who makes processor chips, is now talking about Power-Performance instead of just Performance, i.e. they are looking for ways to increase the ratio between performance and power instead of just ways to increase performance.

It just so happens, that a multi-processor or multi-core machine is much better at power-performance ratio than a single processor machine with the same performance. This is why every computer we buy these days it at least dual-core. A dual-core machine can perform two computing tasks simultaneously. Quad-core machines are also becoming common nowadays.

This makes our lives, as programmers, more interesting. Traditionally, the programs that we write are sequential. The program is interpreted as a sequence of instructions that are executed one after the other on the CPU. If we keep doing what we’ve always done, and not respect the fact that our computer has now more than one computing core, we will effectively use only one half (or one quarter) of the computing power in our hands. It is crucial, for many applications, to harness all of the available computing power.

Scalability is also an issue. In the context of multi-core machine, scalability means that the same program should run faster on a machine with more processing cores. As a software provider, you want to be able to tell your customers that if they are unhappy with the performance, they can buy a stronger computer, and then they’ll get better performance. Since computers are not improving their frequency any more – only the number of processing cores, scalability is not trivial at all.


Any program with user interface works as follows: Wait for input (e.g. from mouse, keyboard, Windows message, etc…), then perform a computation, show some output, and go back to a state of waiting for input. In a single-threaded program, if the computation is taking 10 seconds, the user will not be able to interact with the application for the duration of 10 seconds. The poor user will not be able to abort the computation in the middle, or to even minimize the window, so even his desktop will not be accessible. At that time, the window will not refresh properly.

A responsive application always allows users to abort computations in the middle, minimize the window, draw properly. Ideally, the user should be able to fully interact with the application, during a lengthy computation. Take Visual Studio as an example. While building (compiling) the project, you can open files, edit them, and of course, stop the build process in the middle.

A good way to achieve such responsiveness, is to allocate different threads for the user interface and for long computations. The threads communicate between them, but run in parallel to one-another.



  1. well understood.

    Comment by Doron Osovlanski — September 6, 2009 @ 2:06 pm | Reply

  2. Some classes of problems have required both multi-threading and multi-core programs; a common example is a server using SMP.
    I’ve been working on multi-threading programs since before 1995 – and those were GUI applications that had to remain “responsive” while processing data in the background. Similarly, server programs have been created using several techniques since that data, and even earlier.

    So a more accurate comment might be that “more types of programs require or would benefit from multi-threading.”

    Comment by Frank LaPiana — October 12, 2009 @ 7:38 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: