14.8. Miscellaneous Functions

14.8.1. The pthread_once function

Synopsis
#include <pthread.h>

int pthread_once (pthread_once_t * once_control , void (*init_routine) (void) );

Description

The pthread_once function ensure the function init_routine runs only once regardless of how many threads call it. The thread that makes the first call to pthread_once succeeds in the call. Subsequent calls from other threads fail.

Returns

The pthread_once function returns zero.

Implementation Notes

None.

14.8.2. The pthread_self function

Synopsis
#include <pthread.h>

pthread_t pthread_self ( );

Description

The pthread_self function returns a pointer to the calling thread.

Returns

The pthread_self function returns the pointer.

Implementation Notes

None.

14.8.3. The pthread_key_create function

Synopsis
#include <pthread.h>

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

Description

The pthread_key_create function create a new key that is visible to all threads. This key can be used with pthread_setspecific and pthread_getspecific to save and retrieve data associated with the a thread. If the function destructor is not NULL, then when a thread terminates, the destructor function will be called with the value associated with the key as the argument.

Returns

The pthread_key_create function returns 0 if successful. Otherwise it will return -1 and set errno to ENOMEM.

Implementation Notes
See Also
Section 14.8.4
Section 14.8.5

14.8.4. The pthread_getspecific function

Synopsis
#include <pthread.h>

any_t pthread_getspecific (pthread_key_t key );

Description

The pthread_getspecific function retrieves the value of a data key for the current thread. If key is not a valid key, then errno is set to EINVAL.

Returns

The pthread_getspecific function returns the value associated with key, or returns NULL if key is invalid.

Implementation Notes

None.

14.8.5. The pthread_setspecific function

Synopsis
#include <pthread.h>

int pthread_setspecific (pthread_key_t key , any_t value );

Description

The pthread_setspecific function associates a value with a data key for the calling thread.

Returns

The pthread_setspecific function returns zero if the call is successful, otherwise it sets errno to EINVAL and returns -1.

Implementation Notes

None.

14.8.6. The pthread_cleanup_push function

Synopsis
#include <pthread.h>

int pthread_cleanup_push (void (*fun) (), any_t arg, cleanup_t new );

Description

The pthread_cleanup_push function places the given function on the top of the thread's cleanup stack.

Returns

The pthread_cleanup_push function returns zero if the call is successful, otherwise it sets errno to EINVAL and returns -1.

Implementation Notes

None.

14.8.7. The pthread_cleanup_pop function

Synopsis
#include <pthread.h>

int pthread_cleanup_pop (int execute );

Description

The pthread_cleanup_pop function pops a function off current thread's cleanup stack and if execute is non-zero, executes the function with the argument given in the corresponding call to pthread_cleanup_push.

Returns

The pthread_cleanup_pop function returns zero if the call is successful, otherwise it sets errno to EINVAL and returns -1.

Implementation Notes

None.