多线程编程指南

设置线程的优先级

在 Solaris 线程中,对于在创建时与父线程具有不同优先级的线程,可以在 SUSPEND 模式下创建。在暂停之后,可以通过使用 thr_setprio(3C) 函数调用来修改线程的优先级。thr_setprio() 完成之后,线程可恢复执行。

争用同步对象时,高优先级的线程优先于低优先级的线程。

thr_setprio 语法

thr_setprio(3C) 可用来将当前进程内 tid 所指定线程的优先级更改为 newprio 所指定的优先级。对于 POSIX 线程,请参见pthread_setschedparam 语法

#include <thread.h>



int thr_setprio(thread_t tid, int newprio)

缺省情况下,线程是基于范围从 0(最不重要)到 127(最重要)的固定优先级来调度的。

thread_t tid;

int ret;

int newprio = 20;



/* suspended thread creation */

ret = thr_create(NULL, NULL, func, arg, THR_SUSPENDED, &tid);



/* set the new priority of suspended child thread */

ret = thr_setprio(tid, newprio);



/* suspended child thread starts executing with new priority */

ret = thr_continue(tid);

thr_setprio 返回值

thr_setprio() 在成功运行后返回 0。如果检测到以下任一情况,thr_setprio() 将失败并返回对应的值。


ESRCH

描述:

tid 指定的值不引用现有的线程。


EINVAL

描述:

priority 的值对于与 tid 相关联的调度类没有意义。