Open System Services System Calls Reference Manual (G06.25+, H06.03+)

pthread_key_create(2) OSS System Calls Reference Manual
NAME
pthread_key_create - Generates a unique thread-specic data key
LIBRARY
G-series native OSS processes: /G/system/sysnn/zsptsrl
H-series OSS processes: /G/system/zdllnnn/zsptdll
SYNOPSIS
#include <spthread.h>
int pthread_key_create(
pthread_key_t *key,
void (*destructor)(void *));
PARAMETERS
key species the location where the new thread-specic data key is to be stored.
destructor species a routine called to destroy a thread-specic data value associated with
the created key when a thread terminates. The argument to the destructor for the
user-specied routine is the non-NULL value associated with a key.
DESCRIPTION
This function generates a unique thread-specic data key that is visible to all threads in the pro-
cess. The key provided by this function is an opaque object used to locate thread-specic data.
Although the same key value can be used by different threads, the values bound to the key by the
pthread_setspecic() function are maintained on a per-thread basis and persist for the life of the
calling thread.
The system imposes a maximum number of thread-specic data keys, equal to the symbolic con-
stant PTHREAD_KEYS_MAX.
Thread-specic data allows client software to associate static information with the current
thread. For example, where a routine declares a variable static in a single-threaded program, a
multithreaded version of the program might create a thread-specic data key to store the same
variable.
This function generates and returns a new key value. The key reserves a cell within each thread.
Each call to this function creates a new cell that is unique within an application invocation. Keys
must be generated from initialization code that is guaranteed to be called only once within each
process. (See the pthread_once(2) reference page either online or in the Open System Services
System Calls Reference Manual for more information.)
When a thread terminates, its thread-specic data is automatically destroyed; however, the key
remains unless it is destroyed by a call to the pthread_key_delete() function. An optional des-
tructor routine can be associated with each key. At thread exit, if a key is associated with a non-
NULL destructor parameter, and if the thread has a non-NULL value associated with that key,
the destructor routine is called with the current associated value as its only argument. The order
in which thread-specic data destructors are called at thread termination is undened.
Before each destructor routine is called, the threads value for the corresponding key is set to
NULL. When each destructor routine is called, the destructor must release all storage associated
with the key; otherwise, the destructor will be called again. After the destructor routines have
been called for all non-NULL values with associated destructor routines, if some non-NULL
values with associated destructor routines still exist, then this sequence of actions is repeated.
This sequences is repeated up to PTHREAD_DESTRUCTOR_ITERATIONS times. If, after
all allowed repetitions of this sequence, non-NULL values for any key with a destructor routine
exist, the system terminates the thread. At this point, any key values that represent allocated
heap are lost.
570 Hewlett-Packard Company 527186-003