Threads Module Overview
Background
Many applications can benefit from the use of concurrency in their implementation. In a concurrent model of execution, an application is divided into two or more processes or threads, each executing its own sequence of statements or instructions. An application may consist of one or more processes and a process may consist of one or more threads. Execution may be distributed on two or more machines in a network, two or more processors in a single machine, or may be interleaved on a single processor.
These separately executing processes or threads must generally compete for access to shared resources and data and must cooperate in order to accomplish their overall task.
Concurrent application development is a complicated task. Some of the critical design decisions to be made include the identification of the number of processes and threads required, their particular responsibilities, and the methods by which they will interact. Also key to this development is the identification of good, legal, or invariant program states and bad or illegal program states. The critical problem is to find and implement a solution that maintains or guarantees good program states while prohibiting bad program states, even in those situations where there may be two or more processes or threads acting on the same resource or data.
In a concurrent environment, maintaining desirable program states is accomplished by limiting or negotiating access to shared resources through use of synchronization. The principal role of synchronization is to prevent undesirable or unanticipated interference between simultaneously executing instruction sequences.
Many of today’s operating systems now provide support for multithreading and synchronization. This support takes the form of a low-level procedural API accessed as C language functions.
Using a native API poses many problems:
The independent nature and loose typing of these low-level functions provide programmers with numerous opportunities for making implementation errors that may only appear at run-time.
The procedural style of usage for these functions is at odds with the object-oriented approach favored by today’s C++ programmer.
The functionality and capabilities of a multithreading API and its mechanisms tend to vary significantly from platform to platform.
The Threads Module attempts to alleviate or eliminate these problems using the implementation features described in the following chapters.