OSF DCE Application Development Guide--Core Components
Thread Concepts and Operations
The pthread_keycreate() routine generates a unique key value that is shared by all
threads in the process. This key is the identifier of a piece of thread-specific data. Each
thread uses the same key value to assign or retrieve a thread-specific value. This keeps
your data separate from other thread-specific data. One call to the pthread_keycreate()
routine creates a cell in all threads. Call this routine to specify a routine to be called to
destroy the context value associated with this key when the thread terminates.
The pthread_setspecific( ) routine associates the address of some data with a specific
key. Multiple threads associate different data (by specifying different addresses) with the
same key. For example, each thread points to a different block of dynamically allocated
memory that it has reserved.
The pthread_getspecific() routine obtains the address of the thread-specific data value
associated with a specified key. Use this routine to locate the data associated with the
current thread’s context.
7.7 Thread Cancellation
Canceling is a mechanism by which one thread terminates another thread (or itself).
When you request that a thread be canceled, you are requesting that it terminate as soon
as possible. However, the target thread can control how quickly it terminates by
controlling its general cancelability and its asynchronous cancelability.
The following is a list of the pthread calls that are cancellation points:
• The pthread_setasynccancel() routine
• The pthread_testcancel() routine
• The pthread_delay_np( ) routine
• The pthread_join() routine
• The pthread_cond_wait() routine
• The pthread_cond_timedwait( ) routine
General cancelability is enabled by default. A thread is canceled only at specific places
in the program; for example, when a call to the pthread_cond_wait() routine is made. If
general cancelability is enabled, request the delivery of any pending cancel request by
using the pthread_testcancel( ) routine. This routine allows you to permit cancellation
to occur at places where it may not otherwise be permitted under general cancelability,
and it is especially useful within very long loops to ensure that cancel requests are
noticed within a reasonable time.
If you disable general cancelability, the thread cannot be terminated by any cancel
request. Disabling general cancelability means that a thread could wait indefinitely if it
does not come to a normal conclusion; therefore, be careful about disabling general
cancelability.
Asynchronous cancelability, when it is enabled, allows cancels to be delivered to the
enabling thread at any time, not only at those times that are permitted when just general
cancelability is enabled. Thus, use asynchronous cancellation primarily during long
124245 Tandem Computers Incorporated 7− 13