Why Multithread?

If you've programmed without using multiple threads for all this time, why should you complicate your life with multithreading now? Threads can improve the responsiveness, structure, and efficiency of your code. In addition, some programs containing concurrent threads may run significantly faster on multiprocessor computers under multiprocessor operating systems like Windows NT¨ since each thread could get full use of its own respective CPU.

Consider an application that monitors the performance and available capacity of a network file server. When the server is heavily loaded, it may take several minutes to get utilization measurements from a critical disk drive. A single-threaded application loop that checks the usage of network links, disk drives, CPU, and memory in sequence would be unable to display any data for minutes at a time. A multithreaded application, on the other hand, with one thread dispatched to measure each device and another thread to display the results, would be able to work around the overloaded drive and promptly update the figures for any devices that were able to report their status in a timely fashion. The result is that the total elapsed time until a set of tasks is accomplished can be the maximum of the individual task times rather than their sum, which can result in significant performance improvements. In cases like this, where partial results are meaningful and complete results may be difficult to obtain, multithreading can mean the difference between a useful application and a useless one.

Figure 1 illustrates the performance improvement that multithreading can provide in a situation where two lengthy disk queries must be performed.

Figure 1 Multithreading and Performance

Threads can also improve application responsiveness by managing the priorities of background tasks (a thread's priority can be changed while it is executing, incidentally). A spreadsheet function could, for instance, create a low-priority thread to recalculate in the background without affecting the speed of scrolling or data entry.

Multithreading provides a framework for managing the asynchronous activities in an application. Each thread can focus on the activities specific to that thread. Powerful synchronization mechanisms, like those provided with MFC, allow the coordination of these separate activities.

On the other hand, it's not wise to use threads indiscriminately. Each thread entails a certain amount of system overhead, so it's a good idea to use them only when they significantly improve your program.