PTHREAD_ATTR_GET_NP(3C) | Standard C Library Functions | PTHREAD_ATTR_GET_NP(3C) |
pthread_attr_get_np
—
get pthread attributes of a running thread
#include
<pthread.h>
int
pthread_attr_get_np
(pthread_t
thread, pthread_attr_t *attr);
The
pthread_attr_get_np
()
function provides a way to get the attributes of the thread
thread after it has been created. This function is
most commonly used to obtain the actual location and size of a thread's
stack.
The attributes pointer,
attr, will be filled in with the current attributes
for the thread. The attributes should be allocated by a call to
pthread_attr_init(3C)
prior to calling the
pthread_attr_get_np
()
function. When attr is done being used, it should be
destroyed through a call to
pthread_attr_destroy(3C).
The attributes of the thread thread will be the same as those passed in at the time pthread_create(3C) was called (or the default set if none were specified), except that the following values will be updated:
If the size of the stack was specified, then it may have been changed to ensure that the required alignment of the platform is satisfied.
Upon successful completion, the
pthread_attr_get_np
() function returns
0. Otherwise, an
error number is returned to indicate the error.
The following program demonstrates how to use this function to get the location and stack size of a newly created thread.
#include <assert.h> #include <errno.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> static pthread_t g_thr; void * print_stackinfo(void *arg) { int ret; pthread_attr_t attr; pthread_t *thrp = arg; void *stk; size_t stksize; if (pthread_attr_init(&attr) != 0) { fprintf(stderr, "failed to init attr: %s\n", strerror(errno)); exit(1); } if (pthread_attr_get_np(*thrp, &attr) != 0) { fprintf(stderr, "failed to get thread attributes: %s\n", strerror(errno)); exit(1); } ret = pthread_attr_getstackaddr(&attr, &stk); assert(ret == 0); ret = pthread_attr_getstacksize(&attr, &stksize); assert(ret == 0); (void) printf("stack base is at %p, it is %d bytes large\n", stk, stksize); return (NULL); } int main(void) { int ret; if ((ret = pthread_create(&g_thr, NULL, print_stackinfo, &g_thr) != 0)) { fprintf(stderr, "failed to create a thread: %s\n", strerror(errno)); exit(1); } pthread_join(g_thr, NULL); return (0); }
The pthread_attr_get_np
() function will
fail if:
EINVAL
ESRCH
pthread_attr_destroy(3C), pthread_attr_getdetachstate(3C), pthread_attr_getguardsize(3C), pthread_attr_getinheritsched(3C), pthread_attr_getschedparam(3C), pthread_attr_getschedpolicy(3C), pthread_attr_getscope(3C), pthread_attr_getstackaddr(3C), pthread_attr_getstacksize(3C), pthread_attr_init(3C), pthread_create(3C), pthread_detach(3C), pthread_getschedparam(3C), pthread_setschedparam(3C), attributes(7), threads(7)
December 2, 2023 | OmniOS |