zoukankan      html  css  js  c++  java
  • 线程让出实验【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
    ",count++);
            rt_thread_yield();
        }
    }
    void yield_test2(void* parameter)
    {
        rt_uint32_t count = 0;
        while(1)
        {
            rt_kprintf("thread test2 count:%d
    ",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

    ……..

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

  • 相关阅读:
    多表查询 left join
    对JS关于对象创建的几种方式的整理
    常见正则表达式
    spring
    富文本编辑器
    Struts2快速入门
    一个MySql Sql 优化技巧分享
    maven
    day3
    Spring MVC
  • 原文地址:https://www.cnblogs.com/zyqgold/p/3963171.html
Copyright © 2011-2022 走看看