| PTHREAD_ATTR_GETGUARDSIZE(3) | Library Functions Manual | PTHREAD_ATTR_GETGUARDSIZE(3) |
pthread_attr_getguardsize,
pthread_attr_setguardsize —
get and set thread guard size
POSIX Threads Library (libpthread, -lpthread)
#include
<pthread.h>
int
pthread_attr_getguardsize(const
pthread_attr_t * restrict attr,
size_t * restrict
guardsize);
int
pthread_attr_setguardsize(pthread_attr_t
*attr, size_t
guardsize);
The
pthread_attr_getguardsize()
and
pthread_attr_setguardsize()
functions get and set guardsize in the
attr object. If guardsize is
larger than 0, the system reserves an additional region of guarded memory of
at least guardsize bytes at the end of the thread's
stack for each new thread created by using attr.
The guarded area is understood to be pages of memory that are protected from read and write access. While the guarded area should be rounded by the system page size, the actual default size is implementation-defined. In NetBSD the default guardsize is given by the vm.thread_guard_size sysctl(7).
The rationale behind guardsize is two-fold:
SIGSEGV signal or experience other comparable
fatal error condition. Note that if a thread allocates large data
structures on stack, it may be necessary to raise the default
guardsize in order to detect stack overflows.If pthread_attr_setstack(3) or pthread_attr_setstackaddr(3) is used to set the stack address attribute in attr, the guard size attribute is ignored and no guard area will be allocated; it is the responsibility of the application to handle the overflow conditions.
If successful, both functions return 0. Otherwise, an error number is returned to indicate the error.
No errors are defined for
pthread_attr_getguardsize().
The pthread_attr_setguardsize() may fail
if:
ENOMEM]Both functions conform to IEEE Std 1003.1-2008 (“POSIX.1”).
Older versions of NetBSD, prior to 10.0, 9.4, and 8.3, incorrectly adjust the stack address by the guard size in threads configured with pthread_attr_setstack(3), instead of ignoring the guard size in that case as POSIX prescribes (see PR lib/57721).
Even if you didn't set a nonzero guard size with
pthread_attr_setguardsize(), the system will choose
a nonzero default guard size.
To work around this in applications that run on older and newer versions of NetBSD, as well as on other operating systems, you can safely set the guard size to zero:
pthread_attr_setguardsize(&attr,
0);| July 2, 2017 | NetBSD 11.0 |