zoukankan      html  css  js  c++  java
  • pthread_setschedparam

     1 #include <stdlib.h>
     2 #include <pthread.h>
     3 #include <stdio.h>
     4 #include <sched.h>
     5 void *consumer(void *p)
     6  {
     7           int i;
     8           printf("start (%d)
    ", (int)p);
     9          for (i = 0;1; i++)
    10         {        
    11                   //if(i%200 == 10)
    12                    printf("<<<<<<<(%d)
    ", (int)p);
    13                         
    14         }
    15 }       
    16  int main(int argc, char *argv[])
    17  {
    18          pthread_t  t1, t2, t3;
    19          struct sched_param sched3;
    20          sched3.__sched_priority = 70;
    21         
    22          pthread_create(&t1, NULL, consumer, (void *)4);
    23          pthread_create(&t2, NULL, consumer, (void *)5);
    24          pthread_create(&t3, NULL, (consumer), (void *)6);
    25         sleep(8);
    26         pthread_setschedparam(t3, SCHED_FIFO, &sched3);
    27         pthread_join(t1, NULL);
    28         pthread_join(t2, NULL);
    29         pthread_join(t3, NULL);
    30        return 0;
    31  } 
    32 //pthread_setschedparam在多线程开发中经常被使用的,它主要用于设置线程的调用策略和优先级。 
    33 /* 运行结果:
    34 前8秒交替打印 <<<<<<<<<<<<<<<<<<<<<(4)  和<<<<<<<<<<<<<<<<<<<<<(5)
    35                 和 <<<<<<<<<<<<<<<<<<<<<(6)
    36 8秒以后只打印 <<<<<<<<<<<<<<<<<<<<<(6)
    37 
    38 注意:如果t3线程用sleep()阻塞自己,其它线程将会被调度执行。
    39 上面的程序在8秒后主线程(执行main)也不会执行,比如在29行增加一个exit(1),如果主线程会执行到29行,整个程序将结束,但实际上还会一直执行t3线程.主线程不会被执行。*/ 
  • 相关阅读:
    $Noip2011/Luogu1311$ 选择客栈
    $Noip2013/Luogu1970$ 花匠 $dp$+思维
    $CF1063B Labyrinth$ $01$最短路/$01BFS$
    $UVA10559 Blocks $区间$dp$
    $SCOI2009 windy$数 数位$dp$
    $cometoj#4 D $求和 不是$dp$
    Manacher算法总结
    题解 P1537 【弹珠】
    题解 P4609 【[FJOI2016]建筑师】
    字符串专题随笔
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/3995809.html
Copyright © 2011-2022 走看看