pthread_getspecific_d8_np() — Get the thread-specific value for a key
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a, draft 8 | both | POSIX(ON) |
Format
#define _OPEN_THREADS
#include <pthread.h>
void *pthread_getspecific_d8_np(pthread_key_t key);
General description
Returns the thread-specific data associated with the specified key for the current thread. If no thread-specific data has been set for key, the NULL value is returned.
Many multithreaded applications require storage shared among threads, where each thread has its own unique value. A thread-specific data key is an identifier, created by a thread, for which each thread in the process can set a unique key value.
pthread_key_t is a storage area where the system places the key identifier. To create a key, a thread uses pthread_key_create(). This returns the key identifier into the storage area of type pthread_key_t. At this point, each of the threads in the application has the use of that key, and can set its own unique value by using pthread_setspecific(). A thread can get its own unique value using pthread_getspecific_d8_np() or pthread_getspecific().
The only difference between pthread_getspecific_d8_np() and pthread_getspecific() is the syntax of the function.
Returned value
When successful, pthread_getspecific_d8_np() returns the thread-specific data value associated with key.
- Error Code
- Description
- EINVAL
- The value for key is not valid.
Example
#ifndef _OPEN_THREADS
#define _OPEN_THREADS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#define threads 3
#define BUFFSZ 48
pthread_key_t key;
void *threadfunc(void *);
void destr_fn(void *);
main() {
int status;
int i;
int threadparm[threads];
pthread_t threadid[threads];
int thread_stat[threads];
if ((status = pthread_key_create(&key, destr_fn )) < 0) {
printf("pthread_key_create failed, errno=%d", errno);
exit(1);
}
/* create 3 threads, pass each its number */
for (i=0; i<threads; i++) {
threadparm[i] = i+1;
status = pthread_create( &threadid[i],
NULL,
threadfunc,
(void *)&threadparm[i]);
if ( status < 0) {
printf("pthread_create failed, errno=%d", errno);
exit(2);
}
}
for ( i=0; i<threads; i++) {
status = pthread_join( threadid[i], (void *)&thread_stat[i]);
if ( status < 0) {
printf("pthread_join failed, thread %d, errno=%d\n", i+1, errno);
}
if (thread_stat[i] != 0) {
printf("bad thread status, thread %d, status=%d\n", i+1,
thread_stat[i]);
}
}
exit(0);
}
void *threadfunc(void *parm) {
int status;
int *void;
int threadnum;
int *tnum;
void *getvalue;
char Buffer[BUFFSZ];
tnum = parm;
threadnum = *tnum;
printf("Thread %d executing\n", threadnum);
if (!(value = malloc(sizeof(Buffer))))
printf("Thread %d could not allocate storage, errno = %d\n",
threadnum, errno);
status = pthread_setspecific(key, (void *) value);
if ( status < 0) {
printf("pthread_setspecific failed, thread %d, errno %d",
threadnum, errno);
pthread_exit((void *)12);
}
printf("Thread %d setspecific value: %d\n", threadnum, value);
getvalue = pthread_getspecific_d8_np(key);
if ( getvalue == NULL) {
printf("pthread_getspecific_d8_np failed, thread %d, errno %d",
threadnum, errno);
pthread_exit((void *)13);
}
pthread_exit((void *)0);
}
void destr_fn(void *parm)
{
printf("Destructor function invoked\n");
free(parm)
}