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)\n", (int)p);

 9          for (i = 0;1; i++)

10         {        

11                   //if(i%200 == 10)

12                    printf("<<<<<<<(%d)\n", (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线程.主线程不会被执行。*/ 

 

你可能感兴趣的:(pthread)