'\" te .\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved. .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved. .\" Portions Copyright (c) 1995 IEEE. All Rights Reserved. .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at .\" http://www.opengroup.org/bookstore/. .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. .\" This notice shall appear on any product containing this material. .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] .TH THR_KEYCREATE 3C "Nov 2, 2007" .SH NAME thr_keycreate, thr_keycreate_once, thr_setspecific, thr_getspecific \- thread-specific data functions .SH SYNOPSIS .LP .nf cc -mt [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ] #include \fBint\fR \fBthr_keycreate\fR(\fBthread_key_t *\fR\fIkeyp\fR, \fBvoid (*\fR\fIdestructor\fR)(void *)); .fi .LP .nf \fBint\fR \fBthr_keycreate_once\fR(\fBthread_key_t *\fR\fIkeyp\fR, \fBvoid (*\fR\fIdestructor\fR)(void *)); .fi .LP .nf \fBint\fR \fBthr_setspecific\fR(\fBthread_key_t\fR \fIkey\fR, \fBvoid *\fR\fIvalue\fR); .fi .LP .nf \fBint\fR \fBthr_getspecific\fR(\fBthread_key_t\fR \fIkey\fR, \fBvoid **\fR\fIvaluep\fR); .fi .SH DESCRIPTION .SS "Create Key" .sp .LP In general, thread key creation allocates a key that locates data specific to each thread in the process. The key is global to all threads in the process, which allows each thread to bind a value to the key once the key has been created. The key independently maintains specific values for each binding thread. The \fBthr_keycreate()\fR function allocates a global \fIkey\fR namespace, pointed to by \fIkeyp\fR, that is visible to all threads in the process. Each thread is initially bound to a private element of this \fIkey\fR, which allows access to its thread-specific data. .sp .LP Upon key creation, a new key is assigned the value \fINULL\fR for all active threads. Additionally, upon thread creation, all previously created keys in the new thread are assigned the value \fINULL.\fR .sp .LP Optionally, a destructor function \fIdestructor\fR can be associated with each \fIkey\fR. Upon thread exit, if a \fIkey\fR has a non-null \fIdestructor\fR function and the thread has a non-null \fIvalue\fR associated with that \fIkey\fR, the \fIdestructor\fR function is called with the current associated \fIvalue\fR. If more than one \fIdestructor\fR exists for a thread when it exits, the order of destructor calls is unspecified. .sp .LP An exiting thread runs with all signals blocked. All thread termination functions, including thread-specific data destructor functions, are called with all signals blocked. .sp .LP The \fBthr_keycreate_once()\fR function is identical to the \fBthr_keycreate()\fR function except that the key pointed to by \fIkeyp\fR must be statically initialized with the value \fBTHR_ONCE_KEY\fR before calling \fBthr_keycreate_once()\fR and the key will be created exactly once. This is equivalent to using \fBpthread_once()\fR to call a onetime initialization function that calls \fBthr_keycreate()\fR to create the data key. .SS "Set Value" .sp .LP Once a key has been created, each thread can bind a new \fIvalue\fR to the key using \fBthr_setspecific()\fR. The values are unique to the binding thread and are individually maintained. These values continue for the life of the calling thread. .sp .LP Proper synchronization of \fIkey\fR storage and access must be ensured by the caller. The \fIvalue\fR argument to \fBthr_setspecific()\fR is generally a pointer to a block of dynamically allocated memory reserved by the calling thread for its own use. See \fBEXAMPLES\fR below. .sp .LP At thread exit, the \fIdestructor\fR function, which is associated at time of creation, is called and it uses the specific key value as its sole argument. .SS "Get Value" .sp .LP \fBthr_getspecific()\fR stores the current value bound to \fIkey\fR for the calling thread into the location pointed to by \fIvaluep\fR. .SH RETURN VALUES .sp .LP If successful, \fBthr_keycreate()\fR, \fBthr_keycreate_once()\fR, \fBthr_setspecific()\fR and \fBthr_getspecific()\fR return 0. Otherwise, an error number is returned to indicate the error. .SH ERRORS .sp .LP If the following conditions occur, \fBthr_keycreate()\fR and \fBthr_keycreate_once()\fR return the corresponding error number: .sp .ne 2 .na \fB\fBEAGAIN\fR\fR .ad .RS 10n The system lacked the necessary resources to create another thread-specific data key. .RE .sp .ne 2 .na \fB\fBENOMEM\fR\fR .ad .RS 10n Insufficient memory exists to create the key. .RE .sp .LP If the following conditions occur, \fBthr_setspecific()\fR returns the corresponding error number: .sp .ne 2 .na \fB\fBENOMEM\fR\fR .ad .RS 10n Insufficient memory exists to associate the value with the key. .RE .sp .LP The \fBthr_setspecific()\fR function returns the corresponding error number: .sp .ne 2 .na \fB\fBEINVAL\fR\fR .ad .RS 10n The \fIkey\fR value is invalid. .RE .SH EXAMPLES .LP \fBExample 1 \fRCall the thread-specific data from more than one thread without special initialization. .sp .LP In this example, the thread-specific data in this function can be called from more than one thread without special initialization. For each argument passed to the executable, a thread is created and privately bound to the string-value of that argument. .sp .in +2 .nf /* cc -mt thisfile.c */ #include #include #include #include void *thread_specific_data(void *); void cleanup(void*); #define MAX_ARGC 20 thread_t tid[MAX_ARGC]; int num_threads; int main(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\en", thr_self(), (char *)tsd); thr_getspecific(key, &tsd); printf("tsd for %d remains %s\en", thr_self(), (char *)tsd); return (NULL); } /* end thread_specific_data */ void cleanup(void *v) { /* application-specific clean-up function */ free(v); } .fi .in -2 .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ Interface Stability Committed _ MT-Level MT-Safe .TE .SH SEE ALSO .sp .LP \fBpthread_once\fR(3C), \fBthr_exit\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5) .SH WARNINGS .sp .LP The \fBthr_getspecific()\fR and \fBthr_setspecific()\fR functions can be called either explicitly or implicitly from a thread-specific data destructor function. Calling \fBthr_setspecific()\fR from a destructor can result in lost storage or infinite loops.