zoukankan      html  css  js  c++  java
  • [RTT例程练习] 1.3 线程让出

    RTT 支持相同优先级,而ucosii 不支持。

    如果一个线程不调用rt-thread_delay() 来让出调度器,那么它就会一直运行,其它线程永远处于就绪态。

    而相同优先级的线程,在初始化或创建时还定义了其单次运行的最长的时间片,强迫其让出调度器。


    这里,使用rt_thread_yield(); 也可让出调度器。

    #include <rtthread.h>
    
    rt_thread_t tid1 = RT_NULL;
    rt_thread_t tid2 = RT_NULL;
    
    static void thread1_entry(void* parameter)
    {
        rt_uint32_t count = 0;
        
        while (1)
        {
            rt_kprintf(" thread1: count = %d\n", count++);
    
            rt_thread_yield();
        }   
    }
    
    static void thread2_entry(void* parameter)
    {
        rt_uint32_t count = 0;
    
        while (1)
        {
            rt_thread_yield();
    
            rt_kprintf(" thread2: count = %d\n", count++);
        }
    }
    
    
    
    /* 两者的优先级必须相同,否则一个线程让出调度器之后进入就绪队列, 
       其优先级仍比另一个高,会继续执行,使得另一个线程始终无法得到
       运行。 */
    int rt_application_init()
    {
        tid1 = rt_thread_create("t1", 
            thread1_entry,
            RT_NULL,
            512,
            6, 10);
        if (tid1 != RT_NULL)
            rt_thread_startup(tid1);
    
        tid2 = rt_thread_create("t2", 
            thread2_entry,
            RT_NULL,
            512,
            6, 10);
        if (tid2 != RT_NULL)
            rt_thread_startup(tid2);
    
        return 0;
    }   
    
    /*@}*/
    


    输出结果为:

    \ | /
    - RT - Thread Operating System
    / | \ 1.1.0 build Aug 10 2012
    2006 - 2012 Copyright by rt-thread team
    thread1: count = 0
    thread1: count = 1
    thread2: count = 0
    thread1: count = 2
    thread2: count = 1
    thread1: count = 3
    thread2: count = 2
    thread1: count = 4
    thread2: count = 3
    thread2: count = 4



  • 相关阅读:
    POJ 2234 Matches Game 尼姆博弈
    复杂问题的简单抽象:魔兽世界中的兔子们
    POJ 2368 巴什博奕
    POJ 1067 取石子游戏 威佐夫博弈
    Codeforces 704A Thor 队列模拟
    Codeforces 703B Mishka and trip
    P1447 [NOI2010]能量采集
    P2652 同花顺
    P2034 选择数字 / P2627 [USACO11OPEN]Mowing the Lawn G
    P2515 [HAOI2010]软件安装
  • 原文地址:https://www.cnblogs.com/lyyyuna/p/4123950.html
Copyright © 2011-2022 走看看