线程的优先级
线程的优先级
- 普通线程:没有经过设置,正常地pthread_create(),pthread_join(),pthread_eixt(),普通线程的优先级别为0,如果几个线程都是普通线程,他们是公平竞争的
- 静态优先级:一旦确定优先级别,就固定了
- 动态优先级:它的级别可通过nice()来改变 The range of the nice value is +19 (lowpriority) to -20 (high priority)
- nice值降低,cpu消耗型(如超高清视频解码),nice值提高,io消耗型(如vi编辑器)
调度策略
FIFO:比较霸道,同等优先级时先进先出,如果要处理的内容太多,占用时间太长,其他线程一直在等待,要等FIFO的线程执行完才到其它线程
RR:轮转,比较有道德、文明,根据时间来办事,即使办不完,也会让其他线程
other:普通线程,公平竞争
调度的相关API原型
//初始化,清零
int pthread_attr_init(pthread_attr_t *attr);
//自己决定调动策略inheritsched:PTHREAD_EXPLICIT_SCHED
int pthread_attr_setinheritsched(pthread_attr_t *attr,
int inheritsched);
//设置调动策略 policy:SCHED_FIFO、SCHED_RR、SCHED_OTHER
int pthread_attr_setschedpolicy