[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
    评论
使用RTT(实时操作系统)来驱动小车运动可以分为以下几个步骤: 1. 配置硬件:首先,需要将RTT与小车硬件进行连接。连接方式包括串口、I2C、SPI等常见的接口。根据小车硬件的接口特性和RTT的支持情况,选择合适的接口进行连接。 2. 编写驱动程序:在RTT中编写小车驱动程序,将控制小车运动的相关操作封装为函数或任务。根据小车硬件的控制方式,实现前进、后退、左转、右转等基本运动功能。同时,可以添加其他功能如速度控制、传感器读取等。 3. 创建任务:使用RTT的任务管理功能,为小车驱动程序创建一个任务。任务是一个独立运行的线程,负责调用小车驱动程序,控制小车的运动。可以根据具体需求设置任务的优先级调度周期。 4. 编译和烧录:将编写好的RTT程序编译生成可执行文件,然后将其烧录到嵌入式系统中。烧录方式取决于硬件平台,可以使用编程器、调试器或通过网络进行烧录。 5. 运行:将烧录好的程序加载到嵌入式系统中,并启动RTTRTT会自动创建任务并开始执行。小车驱动程序会在任务中被调用,从而控制小车的运动。 通过以上步骤,就可以使用RTT来驱动小车运动。使用RTT的优势是能够实现实时性要求高的控制任务,提供了任务管理和调度功能,使得驱动程序能够按照要求的周期进行运行。同时,RTT还支持多任务、优先级调度等功能,能够灵活应对不同的应用场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值