免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3201 | 回复: 1
打印 上一主题 下一主题

[C] LINUX- C Pthread 线程优先级的问题.... [复制链接]

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-18 14:21 |只看该作者 |倒序浏览
本帖最后由 hanzhenlll 于 2012-07-18 14:23 编辑

我本来想给别人提供下线程优先的例子,想自测一下,结果死活不通,为什么不能优先呢......... 变量名乱打的  请忽略.....
FIFO RR都试过,下午头晕晕 请教大牛给分析下,怎么让其中一个线程优先级高一些
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>

  4. void *fun_1 (const char *arg)
  5. {
  6.         int i = 1, j =0;
  7.         float sum = 1.007;
  8.         pthread_detach (pthread_self());
  9.         while (1)
  10.         {
  11.         usleep (1);
  12.         printf ("sum 1-----111----->%3f\n", sum);
  13.         }
  14.         pthread_exit (NULL);
  15. }
  16. void *fun_2 (const char *arg)
  17. {
  18.         int i = 1, j = 0;
  19.         float sum = 1.008;
  20.         pthread_detach (pthread_self());
  21.         while (1)
  22.         {
  23.         usleep (1);

  24.         printf ("sum 2-----222----->%3f\n", sum);
  25.         }
  26.         pthread_exit (NULL);
  27. }
  28. int main (void)
  29. {
  30.         int abc;

  31.         pthread_attr_t attr;
  32.         struct sched_param param;
  33.         pthread_attr_init (&attr);
  34. //        pthread_attr_setinheritsched (&attr,&);
  35.         //pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
  36.         pthread_attr_setschedpolicy (&attr, SCHED_RR);
  37.         param.sched_priority = 10;
  38.         pthread_attr_setschedparam (&attr, &param);

  39. #if 1
  40. #endif
  41.         pthread_t pid_1, pid_2;

  42.         int i = 0, j;
  43. //        while (i++ < 10)
  44. //        {
  45.         pthread_create (&pid_1, &attr, fun_1, NULL);
  46.         pthread_attr_destroy (&attr);
  47. #if 0
  48.         pthread_attr_t attr2;
  49.         struct sched_param param2;
  50.         pthread_attr_init (&attr2);
  51. //        pthread_attr_setinheritsched (&attr2,PTHREAD_EXPLICIT_SEHED);
  52.         pthread_attr_setschedpolicy (&attr2, SCHED_RR);
  53.         param2.sched_priority = 90;
  54.         pthread_attr_setschedparam (&attr2, &param2);
  55. #endif
  56.         pthread_create (&pid_2, NULL, fun_2, NULL);
  57. //        pthread_attr_destroy (&attr2);

  58.         //pthread_join (pid_1, NULL);
  59.         //pthread_join (pid_2, NULL);
  60. //        }
  61.         getchar ();
  62.         return 0;
  63. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-08-17 14:32 |只看该作者
这些天也研究了这个优先级的问题,在网上找了些资料,自己也测试了一下可行。
在pthread_attr_setschedpolicy之前调用pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED)就可以了。
PTHREAD_EXPLICIT_SCHED表示使用在schedpolicy和schedparam属性中显式设置的调度策略和参数,
如果不设置这项的话则表示新线程将继承创建线程的调度策略和参数。
使用pthread_attr_setschedparam进行设置的时候值越大代表优先级也越高。
编译的时候一定要加-lpthread选项,不然设置调度策略是不起作用的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP