OSF DCE Application Development Guide--Core Components

OSF DCE Application Development Guide—Core Components
Terminated
The thread has completed all of its work.
Figure 7-1 shows the transitions between states for a typical thread implementation.
Figure 7-1. Thread State Transitions
Waiting Ready Running Terminated
The operations that you can perform include starting, waiting for, terminating, and
deleting threads.
7.1.1 Starting a Thread
To start a thread, create it using the pthread_create() routine. This routine creates the
thread, assigns specified or default attributes, and starts execution of the function you
specified as the thread’s start routine. A unique identifier (handle) for that thread is
returned from the pthread_create( ) routine.
7.1.2 Terminating a Thread
A thread exists until it terminates and the pthread_detach() routine is called for the
thread. The pthread_detach( ) routine can be called for a thread before or after it
terminates. If the thread terminates before pthread_detach() is called for it, then the
thread continues to exist and can be synchronized (joined) until it is detached. Thus, the
object (thread) can be detached by any thread that has access to a handle to the object.
Note that pthread_detach( ) must be called to release the memory allocated for the
thread objects so that this storage does not build up and cause the process to run out of
memory. For example, after a thread returns from a call to join, it detaches the joined-to
thread if no other threads join with it. Similarly, if a thread has no other threads joining
with it, it detaches itself so that its thread object is deallocated as soon as it terminates.
A thread terminates for any of the following reasons:
The thread returns from its start routine; this is the usual case.
The thread calls the pthread_exit( ) routine.
The pthread_exit( ) routine terminates the calling thread and returns a status value,
indicating the thread’s exit status to any potential joiners.
The thread is canceled by a call to the pthread_cancel() routine.
The pthread_cancel() routine requests termination of a specified thread if
cancellation is permitted. (See Section 7.7 for more information on canceling threads
and controlling whether or not cancellation is permitted.)
7 2 Tandem Computers Incorporated 124245