#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <pthread.h> int pthread_getattr_default_np(pthread_attr_t *attr); int pthread_setattr_default_np(pthread_attr_t *attr); Compile and link with -pthread.
The pthread_getattr_default_np() function initializes the thread attributes object referred to by attr so that it contains the default attributes used for thread creation.
Interface | Attribute | Value |
pthread_getattr_default_np(), pthread_setattr_default_np() | Thread safety | MT-Safe |
$ ./a.out Stack size: 8388608 Guard size: 4096 Scheduling policy: SCHED_OTHER Scheduling priority: 0 Detach state: JOINABLE Inherit scheduler: INHERIT
#define errExitEN(en, msg) \
do { errno = en; perror(msg); \
exit(EXIT_FAILURE); } while (0)
static void
display_pthread_attr(pthread_attr_t *attr)
{
int s;
size_t stacksize;
size_t guardsize;
int policy;
struct sched_param schedparam;
int detachstate;
int inheritsched;
s = pthread_attr_getstacksize(attr, &stacksize);
if (s != 0)
errExitEN(s, "pthread_attr_getstacksize");
printf("Stack size: %zd\n", stacksize);
s = pthread_attr_getguardsize(attr, &guardsize);
if (s != 0)
errExitEN(s, "pthread_attr_getguardsize");
printf("Guard size: %zd\n", guardsize);
s = pthread_attr_getschedpolicy(attr, &policy);
if (s != 0)
errExitEN(s, "pthread_attr_getschedpolicy");
printf("Scheduling policy: %s\n",
(policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" : "[unknown]");
s = pthread_attr_getschedparam(attr, &schedparam);
if (s != 0)
errExitEN(s, "pthread_attr_getschedparam");
printf("Scheduling priority: %d\n", schedparam.sched_priority);
s = pthread_attr_getdetachstate(attr, &detachstate);
if (s != 0)
errExitEN(s, "pthread_attr_getdetachstate");
printf("Detach state: %s\n",
(detachstate == PTHREAD_CREATE_DETACHED) ? "DETACHED" :
(detachstate == PTHREAD_CREATE_JOINABLE) ? "JOINABLE" :
"???");
s = pthread_attr_getinheritsched(attr, &inheritsched);
if (s != 0)
errExitEN(s, "pthread_attr_getinheritsched");
printf("Inherit scheduler: %s\n",
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
"???");
}
int
main(int argc, char *argv[])
{
int s;
pthread_attr_t attr;
s = pthread_getattr_default_np(&attr);
if (s != 0)
errExitEN(s, "pthread_getattr_default_np");
display_pthread_attr(&attr);