PTHREAD_ATTR_GET_NP(3C) | Standard C Library Functions | PTHREAD_ATTR_GET_NP(3C) |
pthread_attr_get_np
—
#include <pthread.h>
int
pthread_attr_get_np
(pthread_t
thread, pthread_attr_t *attr);
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.
pthread_attr_get_np
()
and pthread_getattr_np
() functions return
0. Otherwise, an error number is returned to indicate the
error.
#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); }
pthread_attr_get_np
() function will fail if:
EINVAL
ESRCH
August 20, 2019 | illumos |