by, 11-13-2011 at 11:59 AM (1144 Views)
Multithreading is a concept where a program is broken into two or more parts called threads and all these threads run in parallel. Multithreading can make programs more responsive and effective and it increases its performance too. For example, today web pages need to display animations with sound effects and text at the same time. If this is done by using the traditional single-threaded event loop, the application will take a lot of time to get loaded, and time is a very crucial factor for web applications.
While running a program without using multithreading, it follows an approach called an event loop with polling. In this approach, a single process takes the control and decides what is to be done next. It uses polling with a single event queue mechanism to decide this. Once the polling mechanism returns with a signal that a particular resource is available, it dispatches the request to the appropriate event handler. Until this event handler returns nothing else can happen. Let us take an example to understand this. Let us suppose that a user, while working in a spreadsheet package, wants to calculate a complex formula and also wants to scroll down. Both of these requests go in the event queue one by one. The first request, i.e., the calculation of these requests go in the queue first. Let us assume that the formula is very complex and takes a lot of time to get calculated. In such a case, the second event will keep asking for the resource, but the polling mechanism will return with a signal that the resource is not free. Therefore, the system would wait till the formula gets calculated and then only the scrolling will start.
Earlier programming languages like C and C++ did not support multithreading since most of the operating systems at that point of time did not support it. Operating systems like Microsoft Windows, Apple Macintosh, etc. were designed around traditional event loop model.