Processes and Threads

A multi-process system is one which allows two or more programs to run at the same time. A multi-threaded system is one which allows a single program to do more than one thing at a time.

A process is an artifact of the operating system. In a robust OS, like Windows NT, a process consists of an allocation of memory, protected from all other memory and processes running at the same time. Each time you start an application under Windows you launch a new process.

A process is roughly equivalent to an executable running on a single CPU.

A given process may have one or more threads of execution. Typically these threads share some resources, perhaps memory or access to files.

Threads are lightweight processes, with little overhead but less protection from one another.

Threads are the mechanism by which your application can appear to do more than one thing at a time. A typical multithreaded application may be writing to the disk while it is reading from the database, at the same time that it is updating its display. Another very important use of threads is in server applications that support multiple users. Each new user is given a separate thread of execution for their data.

Threading, done well, can greatly increase the performance of your application. If the system would otherwise be waiting to print or to read data from the disk, the ability to continue working can be a great benefit.

On the other hand, many applications are actually slowed down by the addition of threads. If two tasks are going to proceed in parallel, in memory and without human intervention, multi-threading can make them both go slower than they otherwise would, as the system must switch back and forth between the two activities. Each switch brings a bit of overhead in setting up and dismantling the necessary structures in memory.

If there is a user interface, even a display, having the two tasks appear to occur simultaneously may be worth the overhead. On the other hand, if the two tasks are independent, simultaneous and invisible, it is almost always more efficient to run them in series with one another, rather than in parallel.

Multi-threading has a significant impact on your application, and you must design for it from the very start. The presence of threads has a dramatic affect on the overall design of the classes and how they interact. The guiding idea proves to be that:

The best way to build large systems successfully is to provide an environment in which each user (human or thread) feels that all of the system resources are available to him.

In this chapter, we will see that the operating system, database, other system components and the persistence framework must all be carefully coordinated to give the programmer this illusion. Programming with this metaphor has proven to be much more reliable than programming with a metaphor of shared resources and contention.

© 1998 by Wrox Press. All rights reserved.