多线程编程指南

获取调度参数

pthread_attr_getschedparam(3C) 将返回由 pthread_attr_setschedparam() 定义的调度参数。

pthread_attr_getschedparam 语法

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, &param);

使用指定的优先级创建线程

创建线程之前,可以设置优先级属性。将使用在 sched_param 结构中指定的新优先级创建子线程。此结构还包含其他调度信息。

创建子线程时建议执行以下操作:

创建具有优先级的线程的示例

示例 3–2 给出了使用不同于其父线程优先级的优先级创建子线程的示例。


示例 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, &param);



/* set the priority; others are unchanged */

param.sched_priority = newprio;



/* setting the new scheduling param */

ret = pthread_attr_setschedparam (&tattr, &param);



/* with new priority specified */

ret = pthread_create (&tid, &tattr, func, arg); 

pthread_attr_getschedparam 返回值

pthread_attr_getschedparam() 成功完成后将返回零。其他任何返回值都表示出现了错误。 如果出现以下情况,该函数将失败并返回对应的值。


EINVAL

描述:

param 的值为 NULLtattr 无效。