| 1 |
'\" t |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH pthread_attr_setstack 3 2025-06-28 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
pthread_attr_setstack, pthread_attr_getstack \- set/get stack |
| 9 |
attributes in thread attributes object |
| 10 |
.SH LIBRARY |
| 11 |
POSIX threads library |
| 12 |
.RI ( libpthread ,\~ \-lpthread ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <pthread.h> |
| 16 |
.P |
| 17 |
.BR "int pthread_attr_setstack(" "size_t stacksize;" |
| 18 |
.BI " pthread_attr_t *" attr , |
| 19 |
.BI " void " stackaddr [ stacksize ], |
| 20 |
.BI " size_t " stacksize ); |
| 21 |
.BI "int pthread_attr_getstack(const pthread_attr_t *restrict " attr , |
| 22 |
.BI " void **restrict " stackaddr , |
| 23 |
.BI " size_t *restrict " stacksize ); |
| 24 |
.fi |
| 25 |
.P |
| 26 |
.RS -4 |
| 27 |
Feature Test Macro Requirements for glibc (see |
| 28 |
.BR feature_test_macros (7)): |
| 29 |
.RE |
| 30 |
.P |
| 31 |
.BR pthread_attr_getstack (), |
| 32 |
.BR pthread_attr_setstack (): |
| 33 |
.nf |
| 34 |
_POSIX_C_SOURCE >= 200112L |
| 35 |
.fi |
| 36 |
.SH DESCRIPTION |
| 37 |
The |
| 38 |
.BR pthread_attr_setstack () |
| 39 |
function sets the stack address and stack size attributes of the |
| 40 |
thread attributes object referred to by |
| 41 |
.I attr |
| 42 |
to the values specified in |
| 43 |
.I stackaddr |
| 44 |
and |
| 45 |
.IR stacksize , |
| 46 |
respectively. |
| 47 |
These attributes specify the location and size of the stack that should |
| 48 |
be used by a thread that is created using the thread attributes object |
| 49 |
.IR attr . |
| 50 |
.P |
| 51 |
.I stackaddr |
| 52 |
should point to the lowest addressable byte of a buffer of |
| 53 |
.I stacksize |
| 54 |
bytes that was allocated by the caller. |
| 55 |
The pages of the allocated buffer should be both readable and writable. |
| 56 |
.P |
| 57 |
The |
| 58 |
.BR pthread_attr_getstack () |
| 59 |
function returns the stack address and stack size attributes of the |
| 60 |
thread attributes object referred to by |
| 61 |
.I attr |
| 62 |
in the buffers pointed to by |
| 63 |
.I stackaddr |
| 64 |
and |
| 65 |
.IR stacksize , |
| 66 |
respectively. |
| 67 |
.SH RETURN VALUE |
| 68 |
On success, these functions return 0; |
| 69 |
on error, they return a nonzero error number. |
| 70 |
.SH ERRORS |
| 71 |
.BR pthread_attr_setstack () |
| 72 |
can fail with the following error: |
| 73 |
.TP |
| 74 |
.B EINVAL |
| 75 |
.I stacksize |
| 76 |
is less than |
| 77 |
.B PTHREAD_STACK_MIN |
| 78 |
(16384) bytes. |
| 79 |
On some systems, this error may also occur if |
| 80 |
.I stackaddr |
| 81 |
or |
| 82 |
.I stackaddr\~+\~stacksize |
| 83 |
is not suitably aligned. |
| 84 |
.P |
| 85 |
POSIX.1 also documents an |
| 86 |
.B EACCES |
| 87 |
error if the stack area described by |
| 88 |
.I stackaddr |
| 89 |
and |
| 90 |
.I stacksize |
| 91 |
is not both readable and writable by the caller. |
| 92 |
.SH ATTRIBUTES |
| 93 |
For an explanation of the terms used in this section, see |
| 94 |
.BR attributes (7). |
| 95 |
.TS |
| 96 |
allbox; |
| 97 |
lbx lb lb |
| 98 |
l l l. |
| 99 |
Interface Attribute Value |
| 100 |
T{ |
| 101 |
.na |
| 102 |
.nh |
| 103 |
.BR pthread_attr_setstack (), |
| 104 |
.BR pthread_attr_getstack () |
| 105 |
T} Thread safety MT-Safe |
| 106 |
.TE |
| 107 |
.SH STANDARDS |
| 108 |
POSIX.1-2008. |
| 109 |
.SH HISTORY |
| 110 |
glibc 2.2. |
| 111 |
POSIX.1-2001. |
| 112 |
.SH NOTES |
| 113 |
These functions are provided for applications that must ensure that |
| 114 |
a thread's stack is placed in a particular location. |
| 115 |
For most applications, this is not necessary, |
| 116 |
and the use of these functions should be avoided. |
| 117 |
(Use |
| 118 |
.BR pthread_attr_setstacksize (3) |
| 119 |
if an application simply requires a stack size other than the default.) |
| 120 |
.P |
| 121 |
When an application employs |
| 122 |
.BR pthread_attr_setstack (), |
| 123 |
it takes over the responsibility of allocating the stack. |
| 124 |
Any guard size value that was set using |
| 125 |
.BR pthread_attr_setguardsize (3) |
| 126 |
is ignored. |
| 127 |
If deemed necessary, |
| 128 |
it is the application's responsibility to allocate a guard area |
| 129 |
(one or more pages protected against reading and writing) |
| 130 |
to handle the possibility of stack overflow. |
| 131 |
.P |
| 132 |
The address specified in |
| 133 |
.I stackaddr |
| 134 |
should be suitably aligned: |
| 135 |
for full portability, align it on a page boundary |
| 136 |
.RI ( sysconf(_SC_PAGESIZE) ). |
| 137 |
.BR posix_memalign (3) |
| 138 |
may be useful for allocation. |
| 139 |
Probably, |
| 140 |
.I stacksize |
| 141 |
should also be a multiple of the system page size. |
| 142 |
.P |
| 143 |
If |
| 144 |
.I attr |
| 145 |
is used to create multiple threads, then the caller must change the |
| 146 |
stack address attribute between calls to |
| 147 |
.BR pthread_create (3); |
| 148 |
otherwise, the threads will attempt to use the same memory area |
| 149 |
for their stacks, and chaos will ensue. |
| 150 |
.SH EXAMPLES |
| 151 |
See |
| 152 |
.BR pthread_attr_init (3). |
| 153 |
.SH SEE ALSO |
| 154 |
.ad l |
| 155 |
.nh |
| 156 |
.BR mmap (2), |
| 157 |
.BR mprotect (2), |
| 158 |
.BR posix_memalign (3), |
| 159 |
.BR pthread_attr_init (3), |
| 160 |
.BR pthread_attr_setguardsize (3), |
| 161 |
.BR pthread_attr_setstackaddr (3), |
| 162 |
.BR pthread_attr_setstacksize (3), |
| 163 |
.BR pthread_create (3), |
| 164 |
.BR pthreads (7) |