pthread_setschedparam(设置线程的优先级)

举报
CodeAllen 发表于 2021/10/30 00:07:10 2021/10/30
【摘要】  在linux下我们可以通过 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);来创建线程,但是如何设置线程的优先级呢?在讨论这个问题的时候,我们先要确定...

 在linux下我们可以通过


  
  1. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  2. void *(*start_routine)(void*), void *arg);
  3. 来创建线程,但是如何设置线程的优先级呢?
  4. 在讨论这个问题的时候,我们先要确定当前线程使用的调度策略,posix提供了
  5. int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);函数来获取所
  6. 使用的调度策略,它们是:
SCHED_FIFO, SCHED_RR 和 SCHED_OTHER。
我们可以使用

  
  1. int sched_get_priority_max(int policy);
  2. int sched_get_priority_min(int policy);
  3. 来获取线程线程可是设置的最大和最小的优先级值,如果调用成功就返回最大和最小的优先级值,否则返回-1
  4. 从我现在运行的linux系统中,我使用下列程序获取了对应三种调度策略中的最大和最小优先级:
  5. policy = SCHED_OTHER
  6. Show current configuration of priority
  7. max_priority = 0
  8. min_priority = 0
  9. Show SCHED_FIFO of priority
  10. max_priority = 99
  11. min_priority = 1
  12. Show SCHED_RR of priority
  13. max_priority = 99
  14. min_priority = 1
  15. Show priority of current thread
  16. priority = 0
  17. Set thread policy
  18. Set SCHED_FIFO policy
  19. policy = SCHED_FIFO
  20. Set SCHED_RR policy
  21. policy = SCHED_RR
  22. Restore current policy
  23. policy = SCHED_OTHER

我们可以看到
SCHED_OTHER
是不支持优先级使用的,而
SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,
数值越大
优先级越高。 从上面的结果我们可以看出,如果程序控制线程的优先级,一般是用
pthread_attr_getschedpolicy来获取系统使用的调度策略,如果是SCHED_OTHER的话,表明当前策略
不支持线程优先级的使用,否则可以。当然所设定的优先级范围必须在最大和最小值之间。我们可以通过
sched_get_priority_max

sched_get_priority_min来获取。
 

可能网友会问,是否我们可以通过


  
  1. int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);来设定自己所需的
  2. 调度策略呢?我觉得是完全可以的(有些系统需要定义
  3. _POSIX_THREAD_PRIORITY_SCHEDULING),只要系统实现了对应的调用策略。
说了半天,我们还没有说,在系统允许使用线程优先级别的时候,如何设置优先级别呢?

  
  1. int pthread_attr_setschedparam(pthread_attr_t *attr,
  2. const struct sched_param *param);
  3. int pthread_attr_getschedparam(const pthread_attr_t *attr,
  4. struct sched_param *param);
上面两个函数分别用于设置线程的优先级,struct sched_param的定义如下

  
  1. struct sched_param
  2. {
  3. int __sched_priority; //所要设定的线程优先级
  4. };

使用的测试程序:


  
  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <sched.h>
  4. #include <assert.h>
  5. using namespace std;
  6. static int get_thread_policy( pthread_attr_t &attr )
  7. {
  8. int policy;
  9. int rs = pthread_attr_getschedpolicy( &attr, &policy );
  10. assert( rs == 0 );
  11. switch ( policy )
  12. {
  13. case SCHED_FIFO:
  14. cout << "policy = SCHED_FIFO" << endl;
  15. break;
  16. case SCHED_RR:
  17. cout << "policy = SCHED_RR" << endl;
  18. break;
  19. case SCHED_OTHER:
  20. cout << "policy = SCHED_OTHER" << endl;
  21. break;
  22. default:
  23. cout << "policy = UNKNOWN" << endl;
  24. break;
  25. }
  26. return policy;
  27. }
  28. static void show_thread_priority( pthread_attr_t &attr, int policy )
  29. {
  30. int priority = sched_get_priority_max( policy );
  31. assert( priority != -1 );
  32. cout << "max_priority = " << priority << endl;
  33. priority = sched_get_priority_min( policy );
  34. assert( priority != -1 );
  35. cout << "min_priority = " << priority << endl;
  36. }
  37. static int get_thread_priority( pthread_attr_t &attr )
  38. {
  39. struct sched_param param;
  40. int rs = pthread_attr_getschedparam( &attr, &param );
  41. assert( rs == 0 );
  42. cout << "priority = " << param.__sched_priority << endl;
  43. return param.__sched_priority;
  44. }
  45. static void set_thread_policy( pthread_attr_t &attr, int policy )
  46. {
  47. int rs = pthread_attr_setschedpolicy( &attr, policy );
  48. assert( rs == 0 );
  49. get_thread_policy( attr );
  50. }
  51. int main( void )
  52. {
  53. pthread_attr_t attr;
  54. struct sched_param sched;
  55. int rs;
  56. rs = pthread_attr_init( &attr );
  57. assert( rs == 0 );
  58. int policy = get_thread_policy( attr );
  59. cout << "Show current configuration of priority" << endl;
  60. show_thread_priority( attr, policy );
  61. cout << "Show SCHED_FIFO of priority" << endl;
  62. show_thread_priority( attr, SCHED_FIFO );
  63. cout << "Show SCHED_RR of priority" << endl;
  64. show_thread_priority( attr, SCHED_RR );
  65. cout << "Show priority of current thread" << endl;
  66. int priority = get_thread_priority( attr );
  67. cout << "Set thread policy" << endl;
  68. cout << "Set SCHED_FIFO policy" << endl;
  69. set_thread_policy( attr, SCHED_FIFO );
  70. cout << "Set SCHED_RR policy" << endl;
  71. set_thread_policy( attr, SCHED_RR );
  72. cout << "Restore current policy" << endl;
  73. set_thread_policy( attr, policy );
  74. rs = pthread_attr_destroy( &attr );
  75. assert( rs == 0 );
  76. return 0;
  77. }

文章来源: allen5g.blog.csdn.net,作者:CodeAllen的博客,版权归原作者所有,如需转载,请联系作者。

原文链接:allen5g.blog.csdn.net/article/details/118683784

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。