pthread_attr_getschedparam(3C) 将返回由 pthread_attr_setschedparam() 定义的调度参数。
int pthread_attr_getschedparam(pthread_attr_t *tattr, const struct sched_param *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);
创建线程之前,可以设置优先级属性。将使用在 sched_param 结构中指定的新优先级创建子线程。此结构还包含其他调度信息。
创建子线程时建议执行以下操作:
获取现有参数
更改优先级
创建子线程
恢复原始优先级
示例 3–2 给出了使用不同于其父线程优先级的优先级创建子线程的示例。
#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); /* with new priority specified */ ret = pthread_create (&tid, &tattr, func, arg);
pthread_attr_getschedparam() 成功完成后将返回零。其他任何返回值都表示出现了错误。 如果出现以下情况,该函数将失败并返回对应的值。
EINVAL
描述:param 的值为 NULL 或 tattr 无效。