线程让出实验【RT-Thread学习笔记 4】

API: rt_thread_yield

线程函数中调用,本线程释放MCU。如果此时有别的相同优先级的任务整处于等待状态,将获得MCU使用权。

线程让出就是给OS增加一个任务调度的机会。

创建两个线程,观察他们的结果:

//线程让出试验
void yield_test1(void* parameter)
{
    rt_uint32_t count = 0;
    while(1)
    {
        rt_kprintf("thread test1 count:%d\n",count++);
        rt_thread_yield();
    }
}
void yield_test2(void* parameter)
{
    rt_uint32_t count = 0;
    while(1)
    {
        rt_kprintf("thread test2 count:%d\n",count++);
        rt_thread_yield();
    }
}

启动他们:

//线程让出实验,两个线程优先级一样。否则在给一次调度机会也是高优先级的任务使用MCU
    tid2 = rt_thread_create("yield1",yield_test1,RT_NULL,2048,10,5);
    if(tid2 != RT_NULL)
        rt_thread_startup(tid2);
    tid2 = rt_thread_create("yield2",yield_test2,RT_NULL,2048,10,5);
    if(tid2 != RT_NULL)
        rt_thread_startup(tid2);
 

看见两个线程轮流输出:

\ | /

- RT - Thread Operating System

/ | \ 2.0.0 build Aug 29 2014

2006 - 2013 Copyright by rt-thread team

thread test1 count:0

thread test2 count:0

thread test1 count:1

thread test2 count:1

thread test1 count:2

thread test2 count:2

thread test1 count:3

thread test2 count:3

thread test1 count:4

thread test2 count:4

thread test1 count:5

thread test2 count:5

……..

如果没有线程让出的操作,情况将是等一个线程时间片结束之后,才会轮到另一个线程输出。不会是轮流输出了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值