| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- thread-specific data functions
cc –mt [flag... ]file... [library... ]#include <thread.h>intthr_keycreate(thread_key_t *keyp,void (*destructor)(void *));
intthr_keycreate_once(thread_key_t *keyp,void (*destructor)(void *));
intthr_setspecific(thread_key_tkey,void *value);
intthr_getspecific(thread_key_tkey,void **valuep);
In general, thread key creation allocates a key that locates data specific to each thread in the process. The key is global toall threads in the process, which allows each thread to bind avalue to the key once the key has been created. The keyindependently maintains specific values for each binding thread. Thethr_keycreate() function allocatesa globalkey namespace, pointed to bykeyp, that is visible toall threads in the process. Each thread is initially bound to aprivate element of thiskey, which allows access to its thread-specific data.
Upon key creation, a new key is assigned the valueNULLfor all active threads. Additionally, upon thread creation, all previously created keysin the new thread are assigned the valueNULL.
Optionally, a destructor functiondestructor can be associated with eachkey.Upon thread exit, if akey has a non-nulldestructor function andthe thread has a non-nullvalue associated with thatkey, thedestructor functionis called with the current associatedvalue. If more than onedestructorexists for a thread when it exits, the order of destructor callsis unspecified.
An exiting thread runs with all signals blocked. All thread termination functions,including thread-specific data destructor functions, are called with all signals blocked.
Thethr_keycreate_once() function is identical to thethr_keycreate() function except that thekey pointed to bykeyp must be statically initialized with the valueTHR_ONCE_KEY before callingthr_keycreate_once() and the key will be created exactly once. This is equivalent to usingpthread_once() to call a onetime initializationfunction that callsthr_keycreate() to create the data key.
Once a key has been created, each thread can bind a newvalue to the key usingthr_setspecific(). The values are unique to thebinding thread and are individually maintained. These values continue forthe life of the calling thread.
Proper synchronization ofkey storage and access must be ensured bythe caller. Thevalue argument tothr_setspecific() is generally a pointer toa block of dynamically allocated memory reserved by the calling thread forits own use. SeeEXAMPLES below.
At thread exit, thedestructor function, which is associated at time ofcreation, is called and it uses the specific keyvalue as its sole argument.
thr_getspecific() stores the current value bound tokey for the calling threadinto the location pointed to byvaluep.
If successful,thr_keycreate(),thr_keycreate_once(),thr_setspecific() andthr_getspecific() return 0. Otherwise, an errornumber is returned to indicate the error.
If the following conditions occur,thr_keycreate() andthr_keycreate_once() return the corresponding errornumber:
The system lacked the necessary resources to create another thread-specific data key.
Insufficient memory exists to create the key.
If the following conditions occur,thr_setspecific() returns the corresponding error number:
Insufficient memory exists to associate the value with the key.
Thethr_setspecific() function returns the corresponding error number:
Thekey value is invalid.
Example 1 Call the thread-specific data from more than one thread without special initialization.
In this example, the thread-specific data in this function can be calledfrom more than one thread without special initialization. For each argument passedto the executable, a thread is created and privately bound tothe string-value of that argument.
/* cc -mt thisfile.c */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <thread.h>void *thread_specific_data(void *);void cleanup(void*);#define MAX_ARGC 20thread_t tid[MAX_ARGC];int num_threads;intmain(int argc, char *argv[]) { int i; num_threads = argc - 1; for (i = 0; i < num_threads; i++) thr_create(NULL, 0, thread_specific_data, argv[i+1], 0, &tid[i]); for (i = 0; i < num_threads; i++) thr_join(tid[i], NULL, NULL); return (0);} /* end main */void *thread_specific_data(void *arg) { static thread_key_t key = THR_ONCE_KEY; char *private_data = arg; void *tsd = NULL; void *data; thr_keycreate_once(&key, cleanup); thr_getspecific(key, &tsd); if (tsd == NULL) { data = malloc(strlen(private_data) + 1); strcpy(data, private_data); thr_setspecific(key, data); thr_getspecific(key, &tsd); } printf("tsd for %d = %s\n", thr_self(), (char *)tsd); thr_getspecific(key, &tsd); printf("tsd for %d remains %s\n", thr_self(), (char *)tsd); return (NULL);} /* end thread_specific_data */voidcleanup(void *v) { /* application-specific clean-up function */ free(v);}Seeattributes(5) for descriptions of the following attributes:
|
pthread_once(3C),thr_exit(3C),attributes(5),standards(5)
Thethr_getspecific() andthr_setspecific() functions can be called either explicitly orimplicitly from a thread-specific data destructor function. Callingthr_setspecific() from a destructorcan result in lost storage or infinite loops.
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |