使用不同的策略调度不同的线程
Different threads being scheduled with different policies
在POSIXapi线程调度中,我们可以让不同的线程有不同的调度策略。
示例:
pthread_attr_setschedpolicy (&attr, SCHED_FIFO); // set policy first in first out
pthread_create(&tid1,&attr,runner,NULL); // create thread with first in first out
pthread_attr_setschedpolicy (&attr, SCHED_RR); // set policy round robin
pthread_create(&tid2,&attr,runner,NULL); // create thread with round robin
在我看来,所有线程都应该遵循一个共同的调度策略(
如果我错了请纠正我),那么上面的代码是如何工作的?
不,线程不需要有共同的调度策略。调度策略控制线程如何在其静态优先级的 运行 可用线程列表中移动,因此可以在单个线程级别控制此行为。 When picking a process to 运行 the scheduler will look for the highest static priority that has 运行nable threads and choose the thread that is head of the list for that static priority level.
对于 SCHED_FIFO
,一旦某个线程到达给定优先级的列表头部,它将停留在那里直到它阻塞或让步。对于 SCHED_RR
,超过其最大时间片的 运行 可用线程将移至其静态优先级列表的末尾。
在POSIXapi线程调度中,我们可以让不同的线程有不同的调度策略。
示例:
pthread_attr_setschedpolicy (&attr, SCHED_FIFO); // set policy first in first out
pthread_create(&tid1,&attr,runner,NULL); // create thread with first in first out
pthread_attr_setschedpolicy (&attr, SCHED_RR); // set policy round robin
pthread_create(&tid2,&attr,runner,NULL); // create thread with round robin
在我看来,所有线程都应该遵循一个共同的调度策略( 如果我错了请纠正我),那么上面的代码是如何工作的?
不,线程不需要有共同的调度策略。调度策略控制线程如何在其静态优先级的 运行 可用线程列表中移动,因此可以在单个线程级别控制此行为。 When picking a process to 运行 the scheduler will look for the highest static priority that has 运行nable threads and choose the thread that is head of the list for that static priority level.
对于 SCHED_FIFO
,一旦某个线程到达给定优先级的列表头部,它将停留在那里直到它阻塞或让步。对于 SCHED_RR
,超过其最大时间片的 运行 可用线程将移至其静态优先级列表的末尾。