多线程编程指南

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);