多线程编程指南

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

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