1.\" $OpenBSD: pthread_key_create.3,v 1.9 2013/06/05 03:44:50 tedu Exp $
2.\"
3.\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by John Birrell.
17.\" 4. Neither the name of the author nor the names of any co-contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\" $FreeBSD: pthread_key_create.3,v 1.5 1999/08/28 00:03:06 peter Exp $
34.\"
35.Dd $Mdocdate: June 5 2013 $
36.Dt PTHREAD_KEY_CREATE 3
37.Os
38.Sh NAME
39.Nm pthread_key_create
40.Nd thread-specific data key creation
41.Sh SYNOPSIS
42.In pthread.h
43.Ft int
44.Fn pthread_key_create "pthread_key_t *key" "void (*destructor)(void *)"
45.Sh DESCRIPTION
46The
47.Fn pthread_key_create
48function creates a thread-specific data key visible to all threads in the
49process.
50Key values provided by
51.Fn pthread_key_create
52are opaque objects used to locate thread-specific data.
53Although the same
54key value may be used by different threads, the values bound to the key
55by
56.Fn pthread_setspecific
57are maintained on a per-thread basis and persist for the life of the calling
58thread.
59.Pp
60Upon key creation, the value NULL is associated with the new key in all
61active threads.
62Upon thread creation, the value NULL is associated with all
63defined keys in the new thread.
64.Pp
65An optional destructor function may be associated with each key value.
66At thread exit, if a key value has a non-NULL destructor pointer, and the
67thread has a non-NULL value associated with the key, the function pointed
68to is called with the current associated value as its sole argument.
69The order of destructor calls is unspecified if more than one destructor exists
70for a thread when it exits.
71.Pp
72If, after all the destructors have been called for all non-NULL values
73with associated destructors, there are still some non-NULL values with
74associated destructors, then the process is repeated.
75If, after at least
76[PTHREAD_DESTRUCTOR_ITERATIONS] iterations of destructor calls for
77outstanding non-NULL values, there are still some non-NULL values with
78associated destructors, the implementation stops calling destructors.
79.Sh RETURN VALUES
80If successful, the
81.Fn pthread_key_create
82function will store the newly created key value at the location specified by
83.Fa key
84and returns zero.
85Otherwise an error number will be returned to indicate the error.
86.Sh ERRORS
87.Fn pthread_key_create
88will fail if:
89.Bl -tag -width Er
90.It Bq Er EAGAIN
91The system lacked the necessary resources to create another thread-specific
92data key, or the system-imposed limit on the total number of keys per process
93[PTHREAD_KEYS_MAX] would be exceeded.
94.It Bq Er ENOMEM
95Insufficient memory exists to create the key.
96.El
97.Sh SEE ALSO
98.Xr pthread_getspecific 3 ,
99.Xr pthread_key_delete 3 ,
100.Xr pthread_setspecific 3
101.Sh STANDARDS
102.Fn pthread_key_create
103conforms to
104.St -p1003.1-96 .
105