PTHREAD_ATTR_SETGUARDSIZE(3)
π pthread_at...etguardsize(3) π
okay bestie, we're in the Library Functions Manual section of the man pages, and today's main character is the guard size attribute of thread attributes objects. yes, that's a real thing, and yes, it's lowkey iconic. grab your pthread.h and let's go π¦
β¨ NAME β¨
pthread_attr_setguardsize, pthread_attr_getguardsize β set/get guard size attribute in thread attributes object
π LIBRARY π
POSIX threads library (libpthread, -lpthread)
πΈ SYNOPSIS πΈ
#include <pthread.h>
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
size_t *restrict guardsize);
π DESCRIPTION π
so pthread_attr_setguardsize() sets the guard size attribute of the thread attributes object that attr points at, and it sets it to whatever number you put in guardsize. simple. slay.
- if
guardsizeis greater than 0, then for every new thread you create usingattr, the system allocates an additional region of at leastguardsizebytes at the end of the thread's stack, and that region is the guard area for the stack π (but fr, go read BUGS below, it's not that simple) - if
guardsizeis 0, then new threads created withattrwon't have a guard area at all. zero. nada. bye β¨
the default guard size is the same as the system page size. no cap.
now here's the plot twist π β if the stack address attribute has already been set in attr (using pthread_attr_setstack(3) or pthread_attr_setstackaddr(3)), which means you, the caller, are the one allocating the thread's stack, then the guard size attribute gets ignored. like, the system does not create a guard area for you. it's on the application to handle stack overflow β maybe by using mprotect(2) to manually define a guard area at the end of that stack you allocated. you chose this life, bestie, own it π
and pthread_attr_getguardsize() hands you back the guard size attribute of the thread attributes object that attr points at, dropping it into the buffer that guardsize points at. cute, tidy, done.
π₯ RETURN VALUE π₯
- on success, these functions return
0π - on error, they return a nonzero error number π
β ERRORS β
POSIX.1 documents an EINVAL error if attr or guardsize is invalid. but on Linux these functions always succeed. always. (that said, portable and future-proof applications should still handle a possible error return, because being prepared is main character energy π)
π«Ά ATTRIBUTES π«Ά
for an explanation of the terms used in this section, see attributes(7).
pthread_attr_setguardsize(),pthread_attr_getguardsize()β Thread safety:MT-Safe
(the original ships this as a table, but tables aren't invited to this party, so here's the same tea in a bullet π)
π STANDARDS π
POSIX.1-2008.
π HISTORY π
glibc 2.1. POSIX.1-2001. vintage, honestly.
π NOTES π
a guard area is made up of virtual memory pages that are protected so that read and write access is prevented. so if a thread overflows its stack into the guard area, then on most hard architectures it receives a SIGSEGV signal β which is the system's way of going "bestie. your stack. it's overflowing." π¨
guard areas start on page boundaries, and the guard size gets internally rounded up to the system page size when creating a thread. (nevertheless, pthread_attr_getguardsize() returns the guard size that was set by pthread_attr_setguardsize(), not the rounded one. it keeps receipts.)
- setting a guard size of
0may be useful to save memory in an application that creates many threads and knows that stack overflow can never occur. π§ - choosing a guard size larger than the default may be necessary for detecting stack overflows if a thread allocates large data structures on the stack.
π BUGS π
as atglibc 2.8, the NPTL threading implementation includes the guard area within the stack size allocation, rather than allocating extra space at the end of the stack, as POSIX.1 requires. (this can result in anEINVALerror frompthread_create(3)if the guard size value is too large, leaving no space for the actual stack.) π
the obsolete LinuxThreads implementation did the right thing, allocating extra space at the end of the stack for the guard area. rip to a real one π―οΈ
π EXAMPLES π
π SEE ALSO π
mmap(2)mprotect(2)pthread_attr_init(3)pthread_attr_setstack(3)pthread_attr_setstacksize(3)pthread_create(3)pthreads(7)
okay that's the whole page, you're officially a guard-size girlie now π
β¨ this was served up by Linux man-pages 6.20, dated 2025-05-17, and that's the vibe check, periodt ππ¦π
π¦ 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