相关文章推荐
豪爽的香烟  ·  RecyclerView.smoothScr ...·  1 年前    · 
腼腆的小刀  ·  visual c++ - c++ ...·  1 年前    · 
18 pthread_t t1, t2, t3; 19 struct sched_param sched3; 20 sched3.__sched_priority = 70 ; 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) 38 注意:如果t3线程用sleep()阻塞自己,其它线程将会被调度执行。 39 上面的程序在8秒后主线程(执行main)也不会执行,比如在29行增加一个exit(1),如果主线程会执行到29行,整个程序将结束,但实际上还会一直执行t3线程.主线程不会被执行。 */