p The .Fa 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 .Fa stackaddr attribute specifies the location of storage to be used for the thread's stack. All pages within the stack described by .Fa stackaddr and .Fa stacksize should be both readable and writable by the thread.
p The behavior is undefined in all functions if the .Fa attr parameter does not refer to an attribute object initialized by using .Xr pthread_attr_init 3 prior to the call. In addition, undefined behavior may follow if the .Fn pthread_attr_getstack function is called before the .Fa stackaddr attribute has been set. .Ss Rationale 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. l -bullet -offset 2n t There is a certain degree of ambiguity in the POSIX standard with respect to thread stack. t The exact behavior of the functions may vary both across machines and operating systems. In particular, the address specified by .Fa stackaddr should be suitably aligned. The system page size, as specified by .Xr sysconf 3 , and the use of .Xr posix_memalign 3 may guarantee some degree of portability. Also .Xr mmap 2 provides means for alignment. t If the application modifies the stack address, it claims also the responsibility of allocating the stack area and guarding it against possible stack overflow. No default guard area will be allocated (see .Xr pthread_attr_getguardsize 3 ) . It may be necessary to manually use .Xr mprotect 2 in order to define a guard area at the end of the allocated stack. t Moreover, if .Fa attr is used to create multiple threads, the stack address must be changed by the application between successive calls to .Xr pthread_create 3 . .El .Sh RETURN VALUES If successful, these functions return 0. Otherwise, an error number is returned to indicate the error. .Sh ERRORS No errors are defined for the three functions that obtain the stack values. The three functions that set the stack values may fail if: l -tag -width Er t Bq Er ENOMEM There was insufficient memory to complete the operation. .El
p The .Fn pthread_attr_setstacksize function may additionally fail if: l -tag -width Er t Bq Er EINVAL The specified .Fa stacksize is less than .Dv PTHREAD_STACK_MIN or exceeds some system-imposed limit. .El .Sh SEE ALSO .Xr pthread_attr 3 , .Xr pthread_attr_setguardsize 3 .Sh STANDARDS All described functions conform to .St -p1003.1-2001 . Note that .Fn pthread_attr_getstackaddr and .Fn pthread_attr_setstackaddr were however removed from the specification in the .St -p1003.1-2008 revision. .Sh BUGS Older versions of .Nx , prior to 10.0, 9.4, and 8.3, incorrectly adjust the stack address by the guard size in threads configured with .Fn pthread_attr_setstack , instead of ignoring the guard size in that case as .Tn POSIX prescribes
o see .Lk https://gnats.NetBSD.org/57721 "PR lib/57721"
c .
p Even if you didn't set a nonzero guard size with .Xr pthread_attr_setguardsize 3 , the system will choose a nonzero default guard size.
p To work around this in applications that run on older and newer versions of .Nx , as well as on other operating systems, you can safely set the guard size to zero: .Dl "pthread_attr_setguardsize(&attr, 0);"