linux下设置线程优先级 http://blog.csdn.net/typhoonzb/article/details/4544278
来源:互联网 发布:c socket多线程编程 编辑:程序博客网 时间:2024/04/27 21:31
在linux下我们可以通过
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);来创建线程,但是如何设置线程的优先级呢?在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);函数来获取所使用的调度策略,它们是:
SCHED_FIFO, SCHED_RR 和 SCHED_OTHER。我们可以使用int sched_get_priority_max(int policy);int sched_get_priority_min(int policy);来获取线程线程可是设置的最大和最小的优先级值,如果调用成功就返回最大和最小的优先级值,否则返回-1。从我现在运行的linux系统中,我使用下列程序获取了对应三种调度策略中的最大和最小优先级: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我们可以看到
SCHED_OTHER
是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大
优先级越高。 从上面的结果我们可以看出,如果程序控制线程的优先级,一般是用pthread_attr_getschedpolicy来获取系统使用的调度策略,如果是SCHED_OTHER的话,表明当前策略不支持线程优先级的使用,否则可以。当然所设定的优先级范围必须在最大和最小值之间。我们可以通过
sched_get_priority_max
和sched_get_priority_min来获取。可能网友会问,是否我们可以通过
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);来设定自己所需的调度策略呢?我觉得是完全可以的(有些系统需要定义
_POSIX_THREAD_PRIORITY_SCHEDULING),只要系统实现了对应的调用策略。说了半天,我们还没有说,在系统允许使用线程优先级别的时候,如何设置优先级别呢?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);
上面两个函数分别用于设置线程的优先级,struct sched_param的定义如下struct sched_param{ int __sched_priority; //所要设定的线程优先级};使用的测试程序:
#include <iostream>
#include <pthread.h>
#include <sched.h>
#include <assert.h>
using namespace std;
static int get_thread_policy( pthread_attr_t &attr )
{
int policy;
int rs = pthread_attr_getschedpolicy( &attr, &policy );
assert( rs == 0 );
switch ( policy )
{
case SCHED_FIFO:
cout << "policy = SCHED_FIFO" << endl;
break;
case SCHED_RR:
cout << "policy = SCHED_RR" << endl;
break;
case SCHED_OTHER:
cout << "policy = SCHED_OTHER" << endl;
break;
default:
cout << "policy = UNKNOWN" << endl;
break;
}
return policy;
}
static void show_thread_priority( pthread_attr_t &attr, int policy )
{
int priority = sched_get_priority_max( policy );
assert( priority != -1 );
cout << "max_priority = " << priority << endl;
priority = sched_get_priority_min( policy );
assert( priority != -1 );
cout << "min_priority = " << priority << endl;
}
static int get_thread_priority( pthread_attr_t &attr )
{
struct sched_param param;
int rs = pthread_attr_getschedparam( &attr, ¶m );
assert( rs == 0 );
cout << "priority = " << param.__sched_priority << endl;
return param.__sched_priority;
}
static void set_thread_policy( pthread_attr_t &attr, int policy )
{
int rs = pthread_attr_setschedpolicy( &attr, policy );
assert( rs == 0 );
get_thread_policy( attr );
}
int main( void )
{
pthread_attr_t attr;
struct sched_param sched;
int rs;
rs = pthread_attr_init( &attr );
assert( rs == 0 );
int policy = get_thread_policy( attr );
cout << "Show current configuration of priority" << endl;
show_thread_priority( attr, policy );
cout << "Show SCHED_FIFO of priority" << endl;
show_thread_priority( attr, SCHED_FIFO );
cout << "Show SCHED_RR of priority" << endl;
show_thread_priority( attr, SCHED_RR );
cout << "Show priority of current thread" << endl;
int priority = get_thread_priority( attr );
cout << "Set thread policy" << endl;
cout << "Set SCHED_FIFO policy" << endl;
set_thread_policy( attr, SCHED_FIFO );
cout << "Set SCHED_RR policy" << endl;
set_thread_policy( attr, SCHED_RR );
cout << "Restore current policy" << endl;
set_thread_policy( attr, policy );
rs = pthread_attr_destroy( &attr );
assert( rs == 0 );
return 0;
}
- linux下设置线程优先级 http://blog.csdn.net/typhoonzb/article/details/4544278
- Linux下线程的调度策略与优先级(二) http://blog.csdn.net/dongliqiang2006/article/details/5837828
- Linux下线程的调度策略与优先级(一) http://blog.csdn.net/dongliqiang2006/article/details/5837824
- 线程调度 http://blog.csdn.net/loyal_baby/article/details/4202083
- Linux线程-互斥锁pthread_mutex_t http://blog.csdn.net/zmxiangde_88/article/details/7998458
- Linux最大线程数及最大进程数 http://blog.csdn.net/yanyiyyy/article/details/6661234
- Linux线程属性总结 http://blog.csdn.net/zsf8701/article/details/7842392
- 线程本地存储(Windows和Linux) http://blog.csdn.net/wzhwho/article/details/6427596
- linux 裁剪 http://blog.csdn.net/jinsen/article/details/4890970
- Linux平台下 vimrc配置(转网友:http://blog.csdn.net/wooin/article/details/1887737)
- linux下C获取文件的大小http://blog.csdn.net/gdujian0119/article/details/6363574
- (转)Linux下管道的原理 http://blog.csdn.net/guang11cheng/article/details/17144907
- Linux下使用netfilter进行IP包解析 http://blog.csdn.net/kingskyleader/article/details/7701140
- windows下绑定线程(进程)到指定的CPU核心 http://blog.csdn.net/beyond_cn/article/details/15813361
- 内核级线程与用户级线程异同 http://blog.csdn.net/yanglovefeng/article/details/7887939
- 内核线程、轻量级进程、用户线程 http://blog.csdn.net/hsly_support/article/details/7397103
- QT线程(一):线程类 http://blog.csdn.net/calm_agan/article/details/6300709
- 内核线程与用户线程的区别 http://blog.csdn.net/wangkechuang/article/details/7883170
- 1035. Password (20)
- acm的一些常见提示及原因
- 用socket方式连接Nginx优化php-fpm性能
- 常见智力算法类面试题分析
- 归并排序
- linux下设置线程优先级 http://blog.csdn.net/typhoonzb/article/details/4544278
- MySQL常用函数
- yum安装PHP7
- Unity中用到的C#补充(五)- 简单的本地存储
- maven简介
- Spring依赖原理
- 分享一个自己做的图片轮播的插件
- weblogic部署缓存问题
- cocos2d-x 3.0后的事件分发机制 使用分析