1PTHREAD_GETSPECIFIC(3P) POSIX Programmer's Manual PTHREAD_GETSPECIFIC(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 pthread_getspecific, pthread_setspecific — thread-specific data manage‐
13 ment
14
16 #include <pthread.h>
17
18 void *pthread_getspecific(pthread_key_t key);
19 int pthread_setspecific(pthread_key_t key, const void *value);
20
22 The pthread_getspecific() function shall return the value currently
23 bound to the specified key on behalf of the calling thread.
24
25 The pthread_setspecific() function shall associate a thread-specific
26 value with a key obtained via a previous call to pthread_key_create().
27 Different threads may bind different values to the same key. These val‐
28 ues are typically pointers to blocks of dynamically allocated memory
29 that have been reserved for use by the calling thread.
30
31 The effect of calling pthread_getspecific() or pthread_setspecific()
32 with a key value not obtained from pthread_key_create() or after key
33 has been deleted with pthread_key_delete() is undefined.
34
35 Both pthread_getspecific() and pthread_setspecific() may be called from
36 a thread-specific data destructor function. A call to pthread_getspe‐
37 cific() for the thread-specific data key being destroyed shall return
38 the value NULL, unless the value is changed (after the destructor
39 starts) by a call to pthread_setspecific(). Calling pthread_setspe‐
40 cific() from a thread-specific data destructor routine may result
41 either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS
42 attempts at destruction) or in an infinite loop.
43
44 Both functions may be implemented as macros.
45
47 The pthread_getspecific() function shall return the thread-specific
48 data value associated with the given key. If no thread-specific data
49 value is associated with key, then the value NULL shall be returned.
50
51 If successful, the pthread_setspecific() function shall return zero;
52 otherwise, an error number shall be returned to indicate the error.
53
55 No errors are returned from pthread_getspecific().
56
57 The pthread_setspecific() function shall fail if:
58
59 ENOMEM Insufficient memory exists to associate the non-NULL value with
60 the key.
61
62 The pthread_setspecific() function shall not return an error code of
63 [EINTR].
64
65 The following sections are informative.
66
68 None.
69
71 None.
72
74 Performance and ease-of-use of pthread_getspecific() are critical for
75 functions that rely on maintaining state in thread-specific data. Since
76 no errors are required to be detected by it, and since the only error
77 that could be detected is the use of an invalid key, the function to
78 pthread_getspecific() has been designed to favor speed and simplicity
79 over error reporting.
80
81 If an implementation detects that the value specified by the key argu‐
82 ment to pthread_setspecific() does not refer to a a key value obtained
83 from pthread_key_create() or refers to a key that has been deleted with
84 pthread_key_delete(), it is recommended that the function should fail
85 and report an [EINVAL] error.
86
88 None.
89
91 pthread_key_create()
92
93 The Base Definitions volume of POSIX.1‐2017, <pthread.h>
94
96 Portions of this text are reprinted and reproduced in electronic form
97 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
98 table Operating System Interface (POSIX), The Open Group Base Specifi‐
99 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
100 Electrical and Electronics Engineers, Inc and The Open Group. In the
101 event of any discrepancy between this version and the original IEEE and
102 The Open Group Standard, the original IEEE and The Open Group Standard
103 is the referee document. The original Standard can be obtained online
104 at http://www.opengroup.org/unix/online.html .
105
106 Any typographical or formatting errors that appear in this page are
107 most likely to have been introduced during the conversion of the source
108 files to man page format. To report such errors, see https://www.ker‐
109 nel.org/doc/man-pages/reporting_bugs.html .
110
111
112
113IEEE/The Open Group 2017 PTHREAD_GETSPECIFIC(3P)