验证RT-THREAD 时间片轮转调度

按照rt-thread编程指南,时间片调度轮转,

int timeslice_sample(void)

{

rt_thread_t tid = RT_NULL;
/* 创 建 线 程 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;
}
static void thread_entry(void* parameter)
{
rt_uint32_t value;
rt_uint32_t count = 0;
value = (rt_uint32_t)parameter;
while (1)
{
if(0 == (count % 5))
{
rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value
, count);
if(count> 200)
return;
}
count++;
}
thread 1 is running ,thread 1 count = 125

thread 1 is rthread 2 is running ,thread 2 count = 0
thread 2 is running ,thread 2 count = 5
thread 2 is running ,thread 2 count = 10
thread 2 is running ,thread 2 count = 15
thread 2 is running ,thread 2 count = 20
thread 2 is running ,thread 2 count = 25
thread 2 is running ,thread 2 count = 30
thread 2 is running ,thread 2 count = 35
thread 2 is running ,thread 2 count = 40
thread 2 is running ,thread 2 count = 45
thread 2 is running ,thread 2 count = 50
thread 2 is running ,thread 2 count = 55
thread 2 is running ,thread 2 count = 60
由运行的计数结果可以看出,线程 2 的运行时间是线程 1 的一半。 

当把时间片的时间设置为0时,线程就是一直暂用直到大于205退出。

当把时间片设置为0时,应该是不启用时间片,这个手册中没有说明,蛮试下。

查看tick 代码,由于初值是0,--0,就是设置成最大值,占用的时间片为系统支持的最大值。

void rt_tick_increase(void)
{
struct rt_thread *thread;
/* 全 局 变 量 rt_tick 自 加 */
++ rt_tick;
/* 检 查 时 间 片 */
thread = rt_thread_self();
-- thread->remaining_tick;
if (thread->remaining_tick == 0)
{
/* 重 新 赋 初 值 */
thread->remaining_tick = thread->init_tick;
/* 线 程 挂 起 */
rt_thread_yield();
}




posted @ 2020-11-30 15:53  xiaoyu_lin  阅读(344)  评论(1编辑  收藏  举报