pthread_attr_setstack(3)
π pthread_attr_setstack β bestie, we're doing manual stacks now π
okay so real talk: this is a page from the Library Functions Manual (yes, section 3, the library functions girlies), and it's about two little functions that let you control exactly where a thread's stack lives. It's giving "I have very specific needs" energy. Let's go. π«Ά
β¨ NAME β¨
pthread_attr_setstack, pthread_attr_getstack β set/get stack attributes in thread attributes object
πΈ LIBRARY πΈ
POSIX threads library (libpthread, -lpthread)
π SYNOPSIS π
#include <pthread.h>
int pthread_attr_setstack(size_t stacksize;
pthread_attr_t *attr,
void stackaddr[stacksize],
size_t stacksize);
int pthread_attr_getstack(const pthread_attr_t *restrict attr,
void **restrict stackaddr,
size_t *restrict stacksize);
π¦ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
pthread_attr_getstack(),pthread_attr_setstack():_POSIX_C_SOURCE >= 200112L
lowkey important: if you don't define that macro, glibc is gonna act like it doesn't know her. π
π DESCRIPTION π
So pthread_attr_setstack() sets the stack address and stack size attributes of the thread attributes object that attr points at β setting them to whatever you passed in stackaddr and stacksize, respectively. And what do those attributes even mean? They specify the location and size of the stack that a thread created with the attributes object attr should use. Like, you're hand-picking the real estate. π
stackaddr should point to the lowest addressable byte of a buffer of stacksize bytes that was allocated by you, the caller. And the pages of that allocated buffer should be both readable and writable. No read-only zones, bestie, she needs full access. β¨
And pthread_attr_getstack()? She's the reverse uno card: she returns the stack address and stack size attributes of the thread attributes object referred to by attr, dropping them into the buffers pointed to by stackaddr and stacksize, respectively. π«
π₯ RETURN VALUE π₯
On success, these functions return 0; on error, they return a nonzero error number. That's it, that's the whole vibe.
π ERRORS π
pthread_attr_setstack() can fail with the following error:
EINVALβstacksizeis less thanPTHREAD_STACK_MIN(16384) bytes. On some systems, this error may also occur ifstackaddrorstackaddr + stacksizeis not suitably aligned.
POSIX.1 also documents an EACCES error if the stack area described by stackaddr and stacksize is not both readable and writable by the caller.
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
- Interface:
pthread_attr_setstack(),pthread_attr_getstack()- Attribute: Thread safety
- Value:
MT-Safe
yes bestie, they're MT-Safe, we love a safe queen π
β STANDARDS β
POSIX.1-2008.
π§ HISTORY π§
glibc 2.2. POSIX.1-2001.
π NOTES π
These functions are provided for applications that must ensure that a thread's stack is placed in a particular location. But for most applications, this is not necessary, and honestly the use of these functions should be avoided. Like, fr fr, don't unless you must. (Use pthread_attr_setstacksize(3) if an application simply requires a stack size other than the default β that's the chill version.)
When an application employs pthread_attr_setstack(), it takes over the responsibility of allocating the stack. Any guard size value that was set using pthread_attr_setguardsize(3) is ignored. If deemed necessary, it is the application's responsibility to allocate a guard area (one or more pages protected against reading and writing) to handle the possibility of stack overflow. So likeβ¦ you wanted to be in charge? You're in charge now. π
The address specified in stackaddr should be suitably aligned: for full portability, align it on a page boundary (sysconf(_SC_PAGESIZE)). posix_memalign(3) may be useful for allocation. Probably, stacksize should also be a multiple of the system page size. Alignment is the moment, no cap. π
π¨ okay this one matters: Ifattris used to create multiple threads, then the caller must change the stack address attribute between calls topthread_create(3); otherwise, the threads will attempt to use the same memory area for their stacks, and chaos will ensue. Not the cute kind of chaos. The crashy kind. π
π EXAMPLES π
See pthread_attr_init(3). She's got the receipts. β¨
π SEE ALSO π
mmap(2), mprotect(2), posix_memalign(3), pthread_attr_init(3), pthread_attr_setguardsize(3), pthread_attr_setstackaddr(3), pthread_attr_setstacksize(3), pthread_create(3), pthreads(7)
and that's the whole page, bestie!! this one's from Linux man-pages 6.20, dated 2025-06-28, and it's the pthread_attr_setstack(3) page π go forth, allocate responsibly, and remember: guard pages are self-care πβ¨πΈ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms