Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

pthread_getattr_np (3C)

Name

pthread_getattr_np - get attributes of thread

Synopsis

#include <pthread.h>

int pthread_getattr_np(pthread_t t, pthread_attr_t *attr);

Description

The pthread_getattr_np() function allocates and initializes a thread attribute object with values representing an existing thread. The values of the fields in the pthread_attr_t structure are usually the same as those provided during the creation of thread by using the pthread_create() function or the thr_create() function. However, the following factors occur:

  • Stack address is often different due to memory alignment or other platform requirements

  • The schedule inheritance will always match the default value

  • The detach or daemon states of a thread may have changed after the thread gets created

Some of these values are subject to change. The pthread_getattr_np() function returns a snapshot of the current state. The thread attributes object returned as attr, is intended for use with the various pthread_attr_get*() functions to extract individual pieces of information such as the stack address. For example, the pthread_attr_getstackaddr() function. The caller is responsible for freeing the memory allocated for attr by calling the pthread_attr_destroy() function.

Return Values

If successful, the pthread_getattr_np() function returns 0. Otherwise, an error number is returned to indicate the error. It is not an error for the target thread to be a zombie thread.

Errors

The pthread_getattr_np() function will fail if:

EFAULT

One or more attributes of thread could not be determined

EINVAL

attr is NULL

ESRCH

The value specified by a thread does not refer to an existing thread

Examples

Example 1 Retrieve a Thread's Stack Address and Size

The following is an example that shows how to retrieve a thread's current stack address and size.

/* cc thisfile.c */
# include <pthread.h>
# include <stdio.h>

int
main(int argc, char *argv[])
{
 pthread_attr_t attr;
 size_t stksize;
 void *stkaddr;

 (void) pthread_getattr_np(pthread_self(), &attr);
 (void) pthread_attr_getstack(&attr, &stkaddr, &stksize);
 (void) pthread_attr_destroy(&attr);

 printf("Stack Address = %p\n", stkaddr);
 printf("Stack Size = %zu\n", stksize);

 return (0);
}

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
MT-Safe

See Also

pthread_attr_init(3C), pthread_attr_destroy(3C), threads(7), attributes(7), standards(7)