4. API 比较细节 |
#include <thread.h>
int thr_getprio (thread_t tid, int *prio)
int thr_setprio (thread_t tid, int prio)
#include <pthread.h>
#include <sched.h>
int pthread_getschedparam (thread_t tid, int *policy, sched_param *param)
int pthread_setschedparam (thread_t tid, int policy, sched_param *param)
在Solaris 中
Solaris 线索中,用一个不是它父亲的优先级创建一个线索,它在SUSPEND模式下创建。当挂起时,用thr_setprio() 函数调用修改线索优先级;然后继续。
thread_t tid;
int ret;
int newprio = 20;
ret = thr_create (NULL, NULL, func, arg, THR_SUSPEND, &tid); /* suspended thread creation */
ret = thr_setprio (tid, newprio); /* set the new priority of suspended child thread */
ret = thr_continue (tid); /* suspended child thread starts executing with new priority */
在pthreads 中
在pthreads 中,在创建线索前,用户可以设置优先级属性。用新的在sched_param结构中说明的优先级创建子线索。sched_param结构包含其它的调度信息。得到已知参数总是一个很好的想法,改变优先级,然后在设置它。
#include <sched.h> pthread_attr_t tattr; pthread_t tid; int ret; int newprio = 20; sched_param param; ret = pthread_attr_init (&tattr); /* initialized with default attributes */ ret = pthread_attr_getschedparam (&tattr, & param ); /* safe to get existing scheduling param */ param.sched_priority = newprio ; /* set the priority, others are unchanged */ ret = pthread_attr_setschedparam (&tattr, & param ); /* setting the new scheduling param */ ret = pthread_create (&tid, &tattr, func, arg); /* with new priority specified */
另一种创建线索的方法,带有不同于它父亲的优先级,是在线索创建前改变父亲线索的优先级,其后恢复父亲的优先级。下面描述得到和设置已存在线索的优先级。
提供两个pthreads 函数以处理pthreads 优先级。pthread_getschedparam() 得到目标线索的策略和与它相连的调度参数。pthread_setschedparam() 设置目标线索的策略和与它相连的调度参数。在目前的实现中,仅支持SCHED_OTHER 调度策略;调度参数仅是优先级。
在Solaris 中
thread_t tid;
int ret;
int priority;
ret = thr_getprio (tid, &priority); /* returns priority of target thread tid */
在pthreads 中
pthread_t tid;
int ret;
sched_param param;
int priority;
int policy;
ret = pthread_getschedparam (tid, &policy, ¶m); /* scheduling parameters of target thread */
priority = schedparam.sched_priority; /* sched_priority contains the priority of the thread */
在Solaris 中
thread_t tid;
int ret;
int priority;
ret = thr_setprio (tid, priority); /* returns priority of target thread tid */
在pthreads 中
pthread_t tid;
int ret;
sched_param param;
int priority;
schedparam.sched_priority = priority; /* sched_priority will be the priority of the thread */
policy = SCHED_OTHER; /* only supported policy, others will result in ENOTSUP */
ret = pthread_setschedparam (tid, policy, param); /* scheduling parameters of target thread */
Copyright: NPACT |