PTHREAD_ATTR_SETSTACKSIZE(3)
π pthread_at...etstacksize(3) β Library Functions Manual π
okay bestie, welcome to the thread-attributes corner of the man pages, it's giving "let me control my thread's stack before it even exists" and honestly? iconic.
so this page is pthread_attr_setstacksize and pthread_attr_getstacksize, and yeah they do exactly what the names say: set and get the stack size attribute in a thread attributes object. π
π LIBRARY
POSIX threads library, aka libpthread, link it with -lpthread. she's been here forever, she's a legend.
β¨ SYNOPSIS
#include <pthread.h>
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
size_t *restrict stacksize);
π DESCRIPTION
pthread_attr_setstacksize() takes the thread attributes object that attr points to and sets its stack size attribute to whatever you put in stacksize. simple as that, no cap.
and what does that attribute even do? it decides the minimum size in bytes that gets allocated for any threads created using that attributes object attr. so it's like you're telling the thread "bestie, this is your floor, at least this much room for your stack, ok?" πΈ
pthread_attr_getstacksize() is the other direction: it reads the stack size attribute out of the thread attributes object pointed to by attr and drops it into the buffer that stacksize points at. peek at what you set. π¦
π«Ά RETURN VALUE
on success, both of these return 0. on error, they return a nonzero error number β note that's an error number, not your usual -1 and errno situation. the vibe is different here. π
π₯ ERRORS
pthread_attr_setstacksize() can fail with this one:
EINVALβ the stack size is less thanPTHREAD_STACK_MIN(16384) bytes. too smol, she said no.
and on some systems, pthread_attr_setstacksize() can also fail with EINVAL if stacksize is not a multiple of the system page size. yep, some systems are picky like that. π
π ATTRIBUTES
for what all these terms mean, go check out attributes(7). here's the rundown:
- Interface:
pthread_attr_setstacksize(),pthread_attr_getstacksize()β Attribute: Thread safety β Value: MT-Safe
MT-Safe means she's safe for multiple threads to call at the same time. periodt. π
β VERSIONS
these functions have been provided since glibc 2.1. old money.
π STANDARDS
POSIX.1-2001, POSIX.1-2008. standards girlies stay winning.
π§ NOTES
- want the deets on the default stack size of new threads? it's all in
pthread_create(3). go look, bestie. - a thread's stack size is fixed at the time of thread creation. like, set in stone. the only exception? only the main thread can dynamically grow its stack. everyone else stays the size they were born with π
pthread_attr_setstack(3)is the one that lets an application set both the size and location of a caller-allocated stack that a thread is going to use. so if you need to place the stack yourself, that's your function.
π BUGS
okay so as at glibc 2.8, if the stacksize you specified is not a multiple of STACK_ALIGN (16 bytes on most architectures), it may get rounded downward β which is a violation of POSIX.1, because POSIX.1 says the allocated stack will be at least stacksize bytes. so you asked for at least this much and got less. not it. π
π EXAMPLES
see pthread_create(3). that's where the example lives, and she serves.
π SEE ALSO
getrlimit(2)pthread_attr_init(3)pthread_attr_setguardsize(3)pthread_attr_setstack(3)pthread_create(3)pthreads(7)
and that's the whole page, bestie β¨ this was brought to you by Linux man-pages 6.20, dated 2025-05-17 β yes she's recent, yes she's fresh, go set your stack sizes responsibly ππ π«Ά
π¦ 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