You can view multiple threads in a program as executing
simultaneously. You cannot make any assumptions about the relative
start or finish times of threads or the sequence in which they
execute. Nevertheless, you can influence the scheduling of threads.
Each thread has its own thread identifier, which allows it to
be uniquely identified. Also associated with a thread are its
scheduling policy and priority, thread-specific data values, and
the required system resources to support a flow of control.
A thread changes states during the course of its execution. A thread
is in one of the following states:
- Waiting-The thread is not eligible to execute because it
is synchronizing with another thread or with an external event,
such as I/O.
- Ready-The thread is eligible to be executed by a
processor.
- Running-The thread is currently being executed by a
processor.
- Terminated-The thread has completed all of its work.
- Note
- A multithreaded program must be
reentrant. Therefore, be sure that your compiler generates reentrant
code before you do multithreading design or coding work. (Digital's
C, C++, Ada, Pascal, and BLISS compilers generate reentrant code by
default.)
If your program is nonreentrant, it may be impossible to keep
the program's threads from interfering with each other. See
Section 3.2.1 for more information about
thread reentrancy.
In general, when using threads, be aware of language common
practices that are inherently not thread-safe. For example, FORTRAN
typically relies heavily upon static storage. These factors need
to be addressed when writing threaded applications and thread-safe
libraries.
Figure 1-3 shows the transitions between
states for a typical thread implementation.