2 .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
4 .\" Copyright 1991, 1992, 1994, The X/Open Company Ltd.
5 .\" 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
6 .\" http://www.opengroup.org/bookstore/.
7 .\" 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.
8 .\" This notice shall appear on any product containing this material.
9 .\" 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.
10 .\" 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.
11 .\" 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]
12 .TH PTHREAD_KEY_CREATE 3C "Nov 2, 2007"
14 pthread_key_create, pthread_key_create_once_np \- create thread-specific data
19 cc -mt [ \fIflag\fR... ] \fIfile\fR... -lpthread [ \fIlibrary\fR... ]
22 \fBint\fR \fBpthread_key_create\fR(\fBpthread_key_t *\fR\fIkey\fR,
23 \fBvoid\fR (*\fIdestructor\fR)(\fBvoid*\fR));
28 \fBint\fR \fBpthread_key_create_once_np\fR(\fBpthread_key_t *\fR\fIkey\fR,
29 \fBvoid\fR (*\fIdestructor\fR)(\fBvoid*\fR));
35 The \fBpthread_key_create()\fR function creates a thread-specific data key
36 visible to all threads in the process. Key values provided by
37 \fBpthread_key_create()\fR are opaque objects used to locate thread-specific
38 data. Although the same key value may be used by different threads, the values
39 bound to the key by \fBpthread_setspecific()\fR are maintained on a per-thread
40 basis and persist for the life of the calling thread.
43 Upon key creation, the value \fINULL\fR is associated with the new key in all
44 active threads. Upon thread creation, the value \fINULL\fR is associated with
45 all defined keys in the new thread.
48 An optional destructor function may be associated with each key value. At
49 thread exit, if a key value has a non-\fINULL\fR destructor pointer, and the
50 thread has a non-\fINULL\fR value associated with that key, the function
51 pointed to is called with the current associated value as its sole argument.
52 Destructors can be called in any order.
55 If, after all the destructors have been called for all keys with
56 non-\fINULL\fR values, there are still some keys with non-\fINULL\fR values,
57 the process will be repeated. If, after at least
58 \fBPTHREAD_DESTRUCTOR_ITERATIONS\fR iterations of destructor calls for
59 outstanding non-\fINULL\fR values, there are still some keys with
60 non-\fINULL\fR values, the process is continued, even though this might result
64 An exiting thread runs with all signals blocked. All thread termination
65 functions, including thread-specific data destructor functions, are called with
69 The \fBpthread_key_create_once_np()\fR function is identical to the
70 \fBpthread_key_create()\fR function except that the key referred to by
71 *\fIkey\fR must be statically initialized with the value
72 \fBPTHREAD_ONCE_KEY_NP\fR before calling \fBpthread_key_create_once_np()\fR,
73 and the key is created exactly once. This function call is equivalent to using
74 \fBpthread_once\fR(3C) to call a onetime initialization function that calls
75 \fBpthread_key_create()\fR to create the data key.
79 If successful, the \fBpthread_key_create()\fR and
80 \fBpthread_key_create_once_np()\fR functions store the newly created key value
81 at *\fIkey\fR and return \fB0\fR. Otherwise, an error number is returned to
86 The \fBpthread_key_create()\fR and \fBpthread_key_create_once_np()\fR
87 functions will fail if:
94 The system lacked the necessary resources to create another thread-specific
95 data key, or the system-imposed limit on the total number of keys per process
96 \fBPTHREAD_KEYS_MAX\fR has been exceeded.
105 Insufficient memory exists to create the key.
110 The \fBpthread_key_create()\fR and \fBpthread_key_create_once_np()\fR functions
111 will not return an error value of \fBEINTR\fR.
114 \fBExample 1 \fRCall thread-specific data in the function from more than one
115 thread without special initialization.
118 In the following example, the thread-specific data in the function can be
119 called from more than one thread without special initialization. For each
120 argument passed to the executable, a thread is created and privately bound to
121 the string-value of that argument.
126 /* cc -mt thisfile.c */
133 static void *thread_function(void *);
134 static void show_tsd(void);
135 static void cleanup(void*);
137 #define MAX_THREADS 20
139 static pthread_key_t tsd_key = PTHREAD_ONCE_KEY_NP;
142 main(int argc, char *argv[])
144 pthread_t tid[MAX_THREADS];
148 if ((num_threads = argc - 1) > MAX_THREADS)
149 num_threads = MAX_THREADS;
150 for (i = 0; i < num_threads; i++)
151 pthread_create(&tid[i], NULL, thread_function, argv[i+1]);
152 for (i = 0; i < num_threads; i++)
153 pthread_join(tid[i], NULL);
158 thread_function(void *arg)
162 pthread_key_create_once_np(&tsd_key, cleanup);
163 data = malloc(strlen(arg) + 1);
165 pthread_setspecific(tsd_key, data);
173 void *tsd = pthread_getspecific(tsd_key);
175 printf("tsd for %d = %s\en", pthread_self(), (char *)tsd);
178 /* application-specific clean-up function */
182 printf("freeing tsd for %d = %s\en", pthread_self(), (char *)tsd);
191 See \fBattributes\fR(5) for descriptions of the following attributes:
199 ATTRIBUTE TYPE ATTRIBUTE VALUE
201 Interface Stability Committed.
210 For \fBpthread_key_create()\fR, see \fBstandards\fR(5).
214 \fBpthread_once\fR(3C), \fBpthread_getspecific\fR(3C),
215 \fBpthread_setspecific\fR(3C), \fBpthread_key_delete\fR(3C),
216 \fBattributes\fR(5), \fBstandards\fR(5)