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
21
23 The pthread_getspecific() function shall return the value currently
24 bound to the specified key on behalf of the calling thread.
25
26 The pthread_setspecific() function shall associate a thread-specific
27 value with a key obtained via a previous call to pthread_key_create().
28 Different threads may bind different values to the same key. These val‐
29 ues are typically pointers to blocks of dynamically allocated memory
30 that have been reserved for use by the calling thread.
31
32 The effect of calling pthread_getspecific() or pthread_setspecific()
33 with a key value not obtained from pthread_key_create() or after key
34 has been deleted with pthread_key_delete() is undefined.
35
36 Both pthread_getspecific() and pthread_setspecific() may be called from
37 a thread-specific data destructor function. A call to pthread_getspe‐
38 cific() for the thread-specific data key being destroyed shall return
39 the value NULL, unless the value is changed (after the destructor
40 starts) by a call to pthread_setspecific(). Calling pthread_setspe‐
41 cific() from a thread-specific data destructor routine may result
42 either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS
43 attempts at destruction) or in an infinite loop.
44
45 Both functions may be implemented as macros.
46
48 The pthread_getspecific() function shall return the thread-specific
49 data value associated with the given key. If no thread-specific data
50 value is associated with key, then the value NULL shall be returned.
51
52 If successful, the pthread_setspecific() function shall return zero;
53 otherwise, an error number shall be returned to indicate the error.
54
56 No errors are returned from pthread_getspecific().
57
58 The pthread_setspecific() function shall fail if:
59
60 ENOMEM Insufficient memory exists to associate the value with the key.
61
62
63 The pthread_setspecific() function may fail if:
64
65 EINVAL The key value is invalid.
66
67
68 These functions shall not return an error code of [EINTR].
69
70 The following sections are informative.
71
73 None.
74
76 None.
77
79 Performance and ease-of-use of pthread_getspecific() are critical for
80 functions that rely on maintaining state in thread-specific data. Since
81 no errors are required to be detected by it, and since the only error
82 that could be detected is the use of an invalid key, the function to
83 pthread_getspecific() has been designed to favor speed and simplicity
84 over error reporting.
85
87 None.
88
90 pthread_key_create(), the Base Definitions volume of
91 IEEE Std 1003.1-2001, <pthread.h>
92
94 Portions of this text are reprinted and reproduced in electronic form
95 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
96 -- Portable Operating System Interface (POSIX), The Open Group Base
97 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
98 Electrical and Electronics Engineers, Inc and The Open Group. In the
99 event of any discrepancy between this version and the original IEEE and
100 The Open Group Standard, the original IEEE and The Open Group Standard
101 is the referee document. The original Standard can be obtained online
102 at http://www.opengroup.org/unix/online.html .
103
104
105
106IEEE/The Open Group 2003 PTHREAD_GETSPECIFIC(3P)