[RTT例程练习] 1.5 优先级相同线程轮转调度

之前说过,相同优先级的线程,在自己的时间片用光之后,会被剥夺调度器,让给同优先级的其他线程。

程序:

#include <rtthread.h>

static struct rt_thread thread1;
static struct rt_thread thread2;
static char thread1_stack[512];
static char thread2_stack[512];

//static rt_uint32_t t1_count = 0;
//static rt_uint32_t t2_count = 0;
static void thread1_entry(void* parameter)
{
    rt_uint8_t i;
    for(i = 0; i < 6; i ++)
    {
        rt_kprintf("Thread11111111111111111111111111:\n\r");
        rt_kprintf("This is \n");
        rt_kprintf("a\n");
        rt_kprintf("demo\n");
        rt_thread_delay(10);
    }
}

static void thread2_entry(void* parameter)
{
    rt_uint8_t j;
    for(j = 0; j <60; j ++)
    {
        rt_kprintf("Thread2:\n\r");
        rt_kprintf("This is \n");
        rt_kprintf("a\n");
        rt_kprintf("demo\n");
    }
}

int rt_application_init()
{
    rt_err_t result;

    result = rt_thread_init(&thread1,
        "t1",
        thread1_entry, RT_NULL,
        &thread1_stack[0], sizeof(thread1_stack),
        6, 10);
    if (result == RT_EOK)
        rt_thread_startup(&thread1);


    result = rt_thread_init(&thread2,
        "t2",
        thread2_entry, RT_NULL,
        &thread2_stack[0], sizeof(thread2_stack),
        6, 5);
    if (result == RT_EOK)
        rt_thread_startup(&thread2);


    return result;
}

/*@}*/     

输出结果:

Thread1:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This isThread11111111111111111111111111:
This is
a
demo

a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
demo
Thread2:
This is
a
Demo

可以看到thread2 的时间片用光,插入了thread1 的打印内容。

转载于:https://www.cnblogs.com/lyyyuna/archive/2013/02/18/4123948.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值