zoukankan      html  css  js  c++  java
  • [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 的打印内容。

  • 相关阅读:
    技术部新年开工第一场会议
    mockjs学习总结(方便前端模拟数据,加快开发效率)
    vue开发后台管理系统小结
    网站代码、图片优化汇总
    前端seo小结,网页代码优化
    Java 并发编程之锁的实现
    python之格式化输出
    python七类之列表元组
    python七类之集合
    函数风云序列之函数初现
  • 原文地址:https://www.cnblogs.com/lyyyuna/p/4123948.html
Copyright © 2011-2022 走看看