int pthread_attr_getschedparam(pthread_attr_t *restrict tattr, const struct sched_param *restrict param);
#include <pthread.h> pthread_attr_t attr; struct sched_param param; int ret; /* get the existing scheduling param */ ret = pthread_attr_getschedparam (&tattr, ¶m);
You can set the priority attribute before creating the thread. The child thread is created with the new priority that is specified in the sched_param structure. This structure also contains other scheduling information.
Example 3–2 shows an example of creating a child thread with a priority that is different from its parent's priority.
#include <pthread.h> #include <sched.h> pthread_attr_t tattr; pthread_t tid; int ret; int newprio = 20; sched_param param; /* initialized with default attributes */ ret = pthread_attr_init (&tattr); /* safe to get existing scheduling param */ ret = pthread_attr_getschedparam (&tattr, ¶m); /* set the priority; others are unchanged */ param.sched_priority = newprio; /* setting the new scheduling param */ ret = pthread_attr_setschedparam (&tattr, ¶m); /* specify explicit scheduling */ ret = pthread_attr_setinheritsched (&tattr, PTHREAD_EXPLICIT_SCHED); /* with new priority specified */ ret = pthread_create (&tid, &tattr, func, arg);