线程优先级的设定pthread_setschedparam
转载:http://blog.csdn.net/hello_wyq/article/details/1108911
参考:
https://www.cnblogs.com/leijiangtao/p/3995809.html //有具体例子参考
http://blog.csdn.net/hello_wyq/article/details/1557707
http://blog.csdn.net/a_ran/article/details/43759729
http://blog.csdn.net/tiger_ibm/article/details/7932386
在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_OTHER Show current configuration of priority max_priority = 0 min_priority = 0 Show SCHED_FIFO of priority max_priority = 99 min_priority = 1 Show SCHED_RR of priority max_priority = 99 min_priority = 1 Show priority of current thread priority = 0 Set thread policy Set SCHED_FIFO policy policy = SCHED_FIFO Set SCHED_RR policy policy = SCHED_RR Restore current policy policy = 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;
}
线程优先级的设定pthread_setschedparam相关推荐
- Linux 线程优先级设置(内含C语言版线程创建、绑定CPU和优先级设置代码)
参考链接: https://blog.csdn.net/wushuomin/article/details/80051295 //详细讲解pthread_create 函数 https://blog. ...
- [改善Java代码]线程优先级只使用三个等级
线程的优先级(priority)决定了线程获得CPU运行的机会,优先级越高获得的运行机会越大,优先级越低获得的机会越小.Java的线程有10个级别(准确的说是11个级别,级别为0的线程是JVM,应用程 ...
- 并发基础(三): java线程优先级小试牛刀
一.概述 在不同的JVM中(JVM也算是一个操作系统),有着不同的CPU调度算法,对于大部分的JVM来说,优先级也是调度算法中的一个参数. 所以,线程优先级在一定程度上,对线程的调度执行顺序有所影响, ...
- linux线程调度函数,Linux调度策略及线程优先级设置
Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务.一旦占用cpu则一直运行.一直运行直到有更高优先级任务到达或自己放弃 3, ...
- 【Java多线程】线程优先级:优先级高,执行机会多
常用的优先级 一般来说,优先级的范围为1-10,但在个别的操作系统上有所不同. 优先级高的线程,在运行的时候将会获得更多的运行机会. 代码 package cn.hanquan.test;import ...
- Java 多线程:线程优先级
1 优先级取值范围 Java 线程优先级使用 1 ~ 10 的整数表示: 最低优先级 1:Thread.MIN_PRIORITY 最高优先级 10:Thread.MAX_PRIORITY 普通优先级 ...
- ceSetThreadPriority设置线程优先级~!
ceSetThreadPriority 一直採用SetThreadPriority,结果今天发帖询问线程时间问题,才突然顿悟...发现SetThreadPriority只设置248-255,也就是说就 ...
- unix设置线程优先级-转
如何在linux/unix中设置线程的优先级 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*sta ...
- 【C/C++调整线程优先级】
线程优先级 前言 一.线程优先级设置 常用函数 1. pthread_attr_init函数: 2. pthread_attr_setinheritsched函数: 3. pthread_attr_s ...
- Android 中设置线程优先级的正确方式(2种方法)
Android 中设置线程优先级的正确方式(2种方法) 在 Android 中,有两种常见的设置线程优先级的方式: 第一种,使用 Thread 类实例的 setPriority 方法,来设置线程优先级 ...
最新文章
- 京东裁员杀红眼了!说要给n+1,员工签字后,公司又反悔了!
- js符号转码_js传特殊字符到controller (java)的转码问题
- 前景检测算法_4(opencv自带GMM)
- 关于日志文件的一些处理
- CG CTF CRYPTO easy!
- CSS块元素水平垂直居中的实现技巧
- linux加密框架 crypto 算法管理 - 算法查找接口 crypto_larval_lookup
- 周鸿祎:希望将互联网基因与汽车制造企业基因进行重组
- [转]WTL的windows mobile环境的配置(vs2008)[最终版,验证通过]
- c语言火车票管理系统360问答,C语言 车票管理系统 前面一点点的程序,不知道报错是什么意思,可以解释一下吗?代码的具体问题是什么...
- Find The Multiple BFS入门
- (完美)华为畅玩7A AUM-AL00的Usb调试模式在哪里打开的步骤
- 顺序表的基本操作实现
- 《老路用得上的商学课》36-40学习笔记
- Vue----组件注册
- 【leetcode】字符串(KMP、滑动窗口)
- python: 内建函式round() 是四舍五入还是五舍六入?
- 看腻了杨幂,热巴,快来抓取上千张美女图片,古装美女看个够
- 05-----关于C++使用VS时出现 error C2248: “xxx“ 无法访问private成员(在“MySpdlog“类中声明)
- 停不下的脚步:IT高管人士的工作实录