首先创建2个优先级相同的线程,使他们的时间片不一样,验证是否按照时间片轮转调度线程。(注:shell线程的优先级也是20)
2个线程的入口代码完全相同,都是thread_entry,如果是新的时间片到来,那就打印相应线程信息。
这2个线程 分别在执行30个时间片后结束。
#include
#define THREAD_STACK_SIZE 1024
#define THREAD_PRIORITY 20
#define THREAD_TIMESLICE 10
/* 线程入口 */
static void thread_entry(void* parameter)
{
rt_uint32_t value;
rt_uint32_t time,old_time,count = 0;
value = (rt_uint32_t)parameter;
while (1)
{
old_time = rt_tick_get();
if(old_time == time)
{
/* 不是新的时间片,就什么也不干 */
}
else
{
/* 一个新的时间片到来,就打印一次,count用来计算现在是第几个时间片 */
rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value , count);
count++;
time = old_time;
if(count > 29)
{
return;
}
}
}
}
int timeslice_sample()
{
rt_thread_t tid;
/* 创建线程1 */
tid = rt_thread_create("thread1",
thread_entry, (void*)1,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid != RT_NULL)
rt_thread_startup(tid);
/* 创建线程2 */
tid = rt_thread_create("thread2",
thread_entry, (void*)2,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE-5);
if (tid != RT_NULL)
rt_thread_startup(tid);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(timeslice_sample, timeslice sample);
---------------------
仿真运行结果:
\ | /
- RT - Thread Operating System
/ | \ 3.1.0 build Aug 27 2018
2006 - 2018 Copyright by rt-thread team
msh >timeslice_sample
msh >thread 1 is running ,thread 1 count = 0
thread 1 is running ,thread 1 count = 1
thread 1 is running ,thread 1 count = 2
thread 1 is running ,thread 1 count = 3
thread 1 is running ,thread 1 count = 4
thread 1 is running ,thread 1 count = 5
thread 1 is running ,thread 1 count = 6
thread 1 is running ,thread 1 count = 7
thread 1 is running ,thread 1 count = 8
thread 1 is running ,thread 1 count = 9
thread 2 is running ,thread 2 count = 0
thread 2 is running ,thread 2 count = 1
thread 2 is running ,thread 2 count = 2
thread 2 is running ,thread 2 count = 3
thread 2 is running ,thread 2 count = 4
thread 1 is running ,thread 1 count = 10
thread 1 is running ,thread 1 count = 11
thread 1 is running ,thread 1 count = 12
thread 1 is running ,thread 1 count = 13
thread 1 is running ,thread 1 count = 14
thread 1 is running ,thread 1 count = 15
thread 1 is running ,thread 1 count = 16
thread 1 is running ,thread 1 count = 17
thread 1 is running ,thread 1 count = 18
thread 1 is running ,thread 1 count = 19
thread 2 is running ,thread 2 count = 5
thread 2 is running ,thread 2 count = 6
thread 2 is running ,thread 2 count = 7
thread 2 is running ,thread 2 count = 8
thread 2 is running ,thread 2 count = 9
thread 1 is running ,thread 1 count = 20
thread 1 is running ,thread 1 count = 21
thread 1 is running ,thread 1 count = 22
thread 1 is running ,thread 1 count = 23
thread 1 is running ,thread 1 count = 24
thread 1 is running ,thread 1 count = 25
thread 1 is running ,thread 1 count = 26
thread 1 is running ,thread 1 count = 27
thread 1 is running ,thread 1 count = 28
thread 1 is running ,thread 1 count = 29
thread 2 is running ,thread 2 count = 10
thread 2 is running ,thread 2 count = 11
thread 2 is running ,thread 2 count = 12
thread 2 is running ,thread 2 count = 13
thread 2 is running ,thread 2 count = 14
thread 2 is running ,thread 2 count = 15
thread 2 is running ,thread 2 count = 16
thread 2 is running ,thread 2 count = 17
thread 2 is running ,thread 2 count = 18
thread 2 is running ,thread 2 count = 19
thread 2 is running ,thread 2 count = 20
thread 2 is running ,thread 2 count = 21
thread 2 is running ,thread 2 count = 22
thread 2 is running ,thread 2 count = 23
thread 2 is running ,thread 2 count = 24
thread 2 is running ,thread 2 count = 25
thread 2 is running ,thread 2 count = 26
thread 2 is running ,thread 2 count = 27
thread 2 is running ,thread 2 count = 28
thread 2 is running ,thread 2 count = 29
线程1执行10个时间片
线程2之执行5个时间片
...
线程1先执行完30个时间片后结束,剩下的只有线程2在执行,到30时也结束
【结论】看来确实是这样的:优先级相同的线程按照时间片轮转调度~
验证完毕