Document revision date: 30 March 2001

## Guide to the POSIX Threads Library

Previous | Contents | Index

## pthread_getsequence_np

> Obtains the unique identifier for the specified thread.

### Syntax

> pthread_getsequence_np(
> thread );

> Argument | Data Type | Access
> --- | --- | ---
> thread | opaque pthread_t | read

C Binding #include <pthread.h>

unsigned long
pthread_getsequence_np (
pthread_t thread);

### Arguments

> #### thread

> Thread whose sequence number is to be obtained.

### Description

> This routine obtains and returns the thread sequence number for the thread identified by the thread object specified in the thread argument.

> The thread sequence number provides a unique identifier for each existing thread. A thread's thread sequence number is never reused while the thread exists, but can be reused after the thread terminates. The debugger interfaces use this sequence number to identify each thread in commands and in display output.

> The result of calling this routine is undefined if the thread argument does not specify a valid thread object.

Return Values No errors are returned. This routine returns the thread sequence number for the thread identified by the thread object specified in the thread argument. The result of calling this routine is undefined if the thread argument does not specify a valid thread.

#### Associated Routines

> `pthread_create()`
> `pthread_self()`

## pthread_getspecific

> Obtains the thread-specific data associated with the specified key.

### Syntax

> pthread_getspecific(
> key );

> Argument | Data Type | Access
> --- | --- | ---
> key | opaque pthread_key_t | read

C Binding #include <pthread.h>

void
*pthread_getspecific (
pthread_key_t key);

### Arguments

> #### key

> The context key identifies the thread-specific data to be obtained.

### Description

> This routine obtains the thread-specific data associated with the specified key for the current thread. Obtain this key by calling the `pthread_key_create()` routine. This routine returns the value currently bound to the specified key on behalf of the calling thread.

> This routine may be called from a thread-specific data destructor function.

Return Values No errors are returned. This routine returns the thread-specific data value associated with the specified key argument. If no thread-specific data value is associated with key, or if key is not defined, then this routine returns a NULL value.

#### Associated Routines

> `pthread_key_create()`
> `pthread_setspecific()`

## pthread_get_expiration_np

> Obtains a value representing a desired expiration time.

### Syntax

> pthread_get_expiration_np(
> delta ,
> abstime );

> Argument | Data Type | Access
> --- | --- | ---
> delta | struct timespec | read
> abstime | struct timespec | write

C Binding #include <pthread.h>

int
pthread_get_expiration_np (
const struct timespec *delta,
struct timespec *abstime);

### Arguments

> #### delta

> Number of seconds and nanoseconds to add to the current system time. (The result is the time in the future.) This result will be placed in abstime.

> #### abstime

> Value representing the absolute expiration time. The absolute expiration time is obtained by adding delta to the current system time. The resulting abstime is in Universal Coordinated Time (UTC).

### Description

> This routine adds a specified interval to the current absolute system time and returns a new absolute time. This new absolute time may then be used as the expiration time in a call to `pthread_cond_timedwait()` .

> The `timespec` structure contains the following two fields:

> - `tv_sec` is an integral number of seconds.
> - `tv_nsec` is an integral number of nanoseconds.

Return Values If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EINVAL] | The value specified by delta is invalid.

#### Associated Routines

> `pthread_cond_timedwait()`

## pthread_join

> pthread_join32(), pthread_join64() The `pthread_join32()` and `pthread_join64()` forms are only valid in 64-bit pointer environments for OpenVMS Alpha. For information regarding 32- and 64-bit pointers, see Appendix B. Ensure that your compiler provides 64-bit support before you use `pthread_join64()` .

> Causes the calling thread to wait for the termination of a specified thread.

### Syntax

> pthread_join(
> thread ,
> value _ptr );

> Argument | Data Type | Access
> --- | --- | ---
> thread | opaque pthread_t | read
> value_ptr | void * | write

C Binding #include <pthread.h>

int
pthread_join (
pthread_t thread,
void **value_ptr);

### Arguments

> #### thread

> Thread whose termination is awaited by the calling routine.

> #### value_ptr

> Return value of the terminating thread (when that thread either calls `pthread_exit()` or returns from its start routine).

### Description

> This routine suspends execution of the calling thread until the specified target thread thread terminates.

> On return from a successful `pthread_join()` call with a non-NULL value_ptr argument, the value passed to `pthread_exit()` is returned in the location referenced by value_ptr, and the terminating thread is detached.

> If more than one thread attempts to join with the same thread, the results are unpredictable.

> A call to `pthread_join()` returns after the target thread terminates. The `pthread_join()` routine is a deferred cancelation point; the target thread will not be detached if the thread blocked in `pthread_join()` is canceled.

> If a thread calls this routine and specifies its own `pthread_t` , a deadlock can result.

> The `pthread_join()` (or `pthread_detach()` ) routine should eventually be called for every thread that is created with the detachstate attribute of its thread object set to `PTHREAD_CREATE_JOINABLE` , so that storage associated with the thread can be reclaimed.

> Note For OpenVMS Alpha systems: The `pthread_join()` routine is defined to `pthread_join64()` if you compile using `/pointer_size=long` . If you do not specify `/pointer_size` , or if you specify `/pointer_size=short` , then `pthread_join()` is defined to be `pthread_join32()` . You can call `pthread_join32()` or `pthread_join64()` instead of `pthread_join()` . The `pthread_join32()` form returns a 32-bit `void *` value in the address to which value_ptr points. The `pthread_join64()` form returns a 64-bit `void *` value. You can call either, or you can call `pthread_join()` . Note that if you call `pthread_join32()` and the thread with which you join returns a 64-bit value, the high 32 bits of which are not 0 (zero), the Threads Library discards those high bits with no warning.

Return Values If an error condition occurs, this routine returns an integer indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EINVAL] | The value specified by thread does not refer to a joinable thread.
[ESRCH] | The value specified by thread does not refer to an existing thread ID.
[EDEADLK] | A deadlock was detected, or thread specifies the calling thread.

#### Associated Routines

> `pthread_cancel()`
> `pthread_create()`
> `pthread_detach()`
> `pthread_exit()`

## pthread_key_create

> Generates a unique thread-specific data key.

### Syntax

> pthread_key_create(
> key ,
> destructor );

> Argument | Data Type | Access
> --- | --- | ---
> key | opaque pthread_key_t | write
> destructor | procedure | read

C Binding #include <pthread.h>

int
pthread_key_create (
pthread_key_t *key,
void (*destructor)(void *));

### Arguments

> #### key

> Location where the new thread-specific data key will be stored.

> #### destructor

> Procedure called to destroy a thread-specific data value associated with the created key when the thread terminates. Note that the argument to the destructor for the user-specified routine is the non-NULL value associated with a key.

### Description

> This routine generates a unique, thread-specific data key that is visible to all threads in the process. The variable key provided by this routine is an opaque object used to locate thread-specific data. Although the same key value can be used by different threads, the values bound to the key by `pthread_setspecific()` are maintained on a per-thread basis and persist for the life of the calling thread. The initial value of the key in all threads is NULL.

> The Threads Library imposes a maximum number of thread-specific data keys, equal to the symbolic constant `PTHREAD_KEYS_MAX` .

> Thread-specific 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-specific data key to store the same variable.

> This routine generates and returns a new key value. The key reserves a cell within each thread. Each call to this routine 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()` description for more information.)

> When a thread terminates, its thread-specific data is automatically destroyed; however, the key remains unless destroyed by a call to `pthread_key_delete()` . An optional destructor function can be associated with each key. At thread exit, if a key has a non-NULL destructor pointer, and the thread has a non-NULL value associated with that key, the destructor function is called with the current associated value as its sole argument. The order in which thread-specific data destructors are called at thread termination is undefined.

> Before each destructor is called, the thread's value for the corresponding key is set to NULL. After the destructors have been called for all non-NULL values with associated destructors, if there are still some non-NULL values with associated destructors, then this sequence of actions is repeated. If there are still non-NULL values for any key with a destructor after four repetitions of this sequence, the thread is terminated. At this point, any key values that represent allocated heap will be lost. Note that this occurs only when a destructor performs some action that creates a new value for some key. Your program's destructor code should attempt to avoid this sort of circularity.

Return Values If an error condition occurs, this routine returns an integer indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EAGAIN] | The system lacked the necessary resources to create another thread-specific data key, or the limit on the total number of keys per process ( `PTHREAD_KEYS_MAX` ) has been exceeded.
[ENOMEM] | Insufficient memory exists to create the key.

#### Associated Routines

> `pthread_getspecific()`
> `pthread_key_delete()`
> `pthread_once()`
> `pthread_setspecific()`

## pthread_key_delete

> Deletes a thread-specific data key.

### Syntax

> pthread_key_delete(
> key );

> Argument | Data Type | Access
> --- | --- | ---
> key | opaque pthread_key_t | write

C Binding #include <pthread.h>

int
pthread_key_delete (
pthread_key_t key);

### Arguments

> #### key

> Context key to be deleted.

### Description

> This routine deletes the thread-specific data key specified by the key argument, which must have been previously returned by `pthread_key_create()` .

> The thread-specific data values associated with key need not be NULL at the time this routine is called. The application must free any application storage or perform any cleanup actions for data structures related to the deleted key or associated thread-specific data in any threads. This cleanup can be done either before or after this routine is called.

> Attempting to use the key after calling this routine results in unpredictable behavior.

> No destructor functions are invoked by this routine. Any destructor functions that may have been associated with key shall no longer be called upon thread exit. `pthread_key_delete()` can be called from within destructor functions.

Return Values If an error condition occurs, this routine returns an integer indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EINVAL] | The key value is not a valid key.

#### Associated Routines

> `pthread_exit()`
> `pthread_getspecific()`
> `pthread_key_create()`

## pthread_key_getname_np

> Obtains the object name from a thread-specific data key object.

### Syntax

> pthread_key_getname_np(
> key ,
> name ,
> len );

> Argument | Data Type | Access
> --- | --- | ---
> key | opaque pthread_key_t | read
> name | char | write
> len | opaque size_t | read

C Binding #include <pthread.h>

int
pthread_key_getname_np (
pthread_key_t *key,
char *name,
size_t len);

### Arguments

> #### key

> Address of the thread-specific data key object whose object name is to be obtained.

> #### name

> Location to store the obtained object name.

> #### len

> Length in bytes of buffer at the location specified by name.

### Description

> This routine copies the object name from the thread-specific data key object specified by the key argument to the buffer at the location specified by the name argument. Before calling this routine, your program must allocate the buffer indicated by name.

> The object name is a C language string and provides an identifier that is meaningful to a person debugging a multithreaded application. The maximum number of characters in the object name is 31.

> If the specified thread-specific data key object has not been previously set with an object name, this routine copies a C language null string into the buffer at location name.

Return Values If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EINVAL] | The value specified by key is not a valid key.

#### Associated Routines

> `pthread_key_setname_np()`

## pthread_key_setname_np

> Changes the object name in a thread-specific data key object.

### Syntax

> pthread_key_setname_np(
> key ,
> name ,
> mbz );

> Argument | Data Type | Access
> --- | --- | ---
> key | opaque pthread_key_t | write
> name | char | read
> mbz | void | read

C Binding #include <pthread.h>

int
pthread_key_setname_np (
pthread_key_t *cond,
const char *name,
void *mbz);

### Arguments

> #### key

> Address of the thread-specific data key object whose object name is to be changed.

> #### name

> Object name value to copy into the key object.

> #### mbz

> Reserved for future use. The value must be zero (0).

### Description

> This routine changes the object name in the thread-specific data key object specified by the key argument to the value specified by the name argument. To set a new thread-specific data key object's object name, call this routine immediately after initializing the key object.

> The object name is a C language string and provides an identifier that is meaningful to a person debugging a multithreaded application. The maximum number of characters in the object name is 31.

Return Values If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows:

Return | Description
--- | ---
0 | Successful completion.
[EINVAL] | The value specified by key is not a valid key, or the length in characters of name exceeds 31.
[ENOMEM] | Insufficient memory exists to create a copy of the object name string.

#### Associated Routines

> `pthread_key_getname_np()`

Previous | Next | Contents | Index

| privacy and legal statement |
6101PRO_020.HTML