Multithreaded Programming Guide

Exit Print View

Updated: July 2014
 
 

Setting Thread-Specific Data

Use pthread_setspecific(3C) to set the thread-specific binding to the specified thread-specific data key.

pthread_setspecific Syntax

int pthread_setspecific(pthread_key_t key, const void *value);
#include <pthread.h>

pthread_key_t key;
void *value;
int ret;

/* key previously created */
ret = pthread_setspecific(key, value); 

pthread_setspecific Return Values

pthread_setspecific() returns zero after completing successfully. Any other return value indicates that an error occurred. When any of the following conditions occur, pthread_setspecific() fails and returns the corresponding value.

ENOMEM

Description: Insufficient virtual memory is available.

EINVAL

Description: key is invalid.


Note - pthread_setspecific() does not free its storage when a new binding is set. The existing binding must be freed, otherwise a memory leak can occur.