| PTHREAD_ATTR_GETSTACK(3) | Library Functions Manual | PTHREAD_ATTR_GETSTACK(3) |
pthread_attr_getstack,
pthread_attr_setstack,
pthread_attr_getstacksize,
pthread_attr_setstacksize,
pthread_attr_getstackaddr,
pthread_attr_setstackaddr —
get and set thread stack attributes
POSIX Threads Library (libpthread, -lpthread)
#include
<pthread.h>
int
pthread_attr_getstack(const
pthread_attr_t * restrict attr,
void ** restrict
stackaddr, size_t *
restrict stacksize);
int
pthread_attr_setstack(pthread_attr_t
* restrict attr, void
*stackaddr, size_t
stacksize);
int
pthread_attr_getstacksize(const
pthread_attr_t * restrict attr,
size_t * restrict
stacksize);
int
pthread_attr_setstacksize(pthread_attr_t
*attr, size_t
stacksize);
int
pthread_attr_getstackaddr(const
pthread_attr_t * restrict attr,
void ** restrict
stackaddr);
int
pthread_attr_setstackaddr(pthread_attr_t
*attr, void
*stackaddr);
The
pthread_attr_getstack()
and
pthread_attr_setstack()
functions get and set, respectively, the thread stack attributes
stackaddr and stacksize in the
attr object. The remaining four functions behave
similarly, but instead of getting or setting both
stackaddr and stacksize, these
get and set the values individually.
The stacksize parameter is defined to be the minimum stack size (in bytes) allocated for the thread's stack during the creation of the thread. The stackaddr attribute specifies the location of storage to be used for the thread's stack. All pages within the stack described by stackaddr and stacksize should be both readable and writable by the thread.
The behavior is undefined in all
functions if the attr parameter does not refer to an
attribute object initialized by using
pthread_attr_init(3)
prior to the call. In addition, undefined behavior may follow if the
pthread_attr_getstack()
function is called before the stackaddr attribute has
been set.
The rationale behind these functions is to address cases where an application may be used in an environment where the stack of a thread must be placed to some particular region of memory. For the majority of applications, this is seldom necessary, and the use of these functions should be generally avoided. At least few potential caveats can be mentioned.
If successful, these functions return 0. Otherwise, an error number is returned to indicate the error.
No errors are defined for the three functions that obtain the stack values. The three functions that set the stack values may fail if:
ENOMEM]The pthread_attr_setstacksize() function
may additionally fail if:
EINVAL]PTHREAD_STACK_MIN or exceeds some system-imposed
limit.All described functions conform to IEEE Std
1003.1-2001 (“POSIX.1”). Note that
pthread_attr_getstackaddr() and
pthread_attr_setstackaddr() were however removed
from the specification in the IEEE Std 1003.1-2008
(“POSIX.1”) revision.
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(),
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(3), 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 9, 2010 | NetBSD 11.0 |