PTHREAD_ATTR_GET_NP(3C) Standard C Library Functions PTHREAD_ATTR_GET_NP(3C)

pthread_attr_get_npget pthread attributes of a running thread

#include <pthread.h>

int
pthread_attr_get_np(pthread_t thread, pthread_attr_t *attr);

The () 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 () 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 no explicit stack size was specified, then attr will contain the actual size of the stack.

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.

If no stack address was specified, then attr will contain the actual address of the stack that the system allocated for the thread.
The detach state, whether or not the thread may be joined by a call to pthread_join(3C), may have changed since the process was created due to a call to pthread_detach(3C). attr will reflect the current setting of thread.
The scheduling parameter attribute will be updated with the current scheduling parameter of thread. This is the same information as available through pthread_getschedparam(3C) and it is the preferred interface for obtaining that information.
The scheduling policy attribute of attr will be updated with the current scheduling policy being applied to the thread. This may have changed, for example, due to a call to pthread_setschedparam(3C). As with the thread's scheduling parameter, the preferred interface for obtaining this information is by using pthread_getschedparam(3C).
The value of the guard size attribute for the thread will be updated to reflect the actual size of the guard installed for thread. For more information on the guard size of a thread and its purpose, see pthread_attr_getguardsize(3C).

Upon successful completion, the pthread_attr_get_np() function returns . 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:

The pthread_attr_t object attr was not properly initialized with a call to pthread_attr_init(3C).
No thread could be found corresponding to the specified thread ID, thread.

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