linux线程优先级设置
1、Linux线程优先级设置: 首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义: int sched_get_priority_max(int policy); int sched_get_priority_min(int policy);

2、注意:SCHED_OTHER 是不支持优先级使用的,而 SCHED_FIFO 和 SCHED_RR 支持优先级的使用,他们分别为1和99,数值越大优先级越高。 设置和获取优先级通过以下两个函数:int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; //设置优先级

3、系统创建线程时,默认的线程是 SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 上面的param使用了下面的这个数据结构:struct sched_param { int __sched_priority; // 所要设定的线程优先级};

5、下面是测试程序的运行结果:policy=SCHED_OTHERShow current configuration of prioritymax_priority=0min_priority=0show SCHED_FIFO of prioritymax_priority=99min_priority=1show SCHED_RR of prioritymax_priority=99min_priority=1show priority of current threadpriority=0Set thread policyset SCHED_FIFO policypolicy= SCHED_FIFOset SCHED_RR policypolicy= SCHED_RRRestore current policypolicy=SCHED_OTHER
