【RT-Thread学习笔记 4】线程让出实验
2016-07-04 15:02
337 查看
API: rt_thread_yield
线程函数中调用,本线程释放MCU。如果此时有别的相同优先级的任务整处于等待状态,将获得MCU使用权。
线程让出就是给OS增加一个任务调度的机会。
创建两个线程,观察他们的结果:
启动他们:
看见两个线程轮流输出:
\ | /
- 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
……..
如果没有线程让出的操作,情况将是等一个线程时间片结束之后,才会轮到另一个线程输出。不会是轮流输出了
线程函数中调用,本线程释放MCU。如果此时有别的相同优先级的任务整处于等待状态,将获得MCU使用权。
线程让出就是给OS增加一个任务调度的机会。
创建两个线程,观察他们的结果:
//线程让出试验 void yield_test1(void* parameter) { rt_uint32_t count = 0; while(1) { rt_kprintf("thread test1 count:%d\n",count++); rt_thread_yield(); } } void yield_test2(void* parameter) { rt_uint32_t count = 0; while(1) { rt_kprintf("thread test2 count:%d\n",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
……..
如果没有线程让出的操作,情况将是等一个线程时间片结束之后,才会轮到另一个线程输出。不会是轮流输出了
相关文章推荐
- 应用领航:盘点那些年我们一起追过的OS
- 无奇不有!盘点各国自己开发的操作系统
- 可自定义oem的萝卜家园 Ghost XP 新春装机版 V200801 下载
- C#实现判断操作系统是否为Win8以上版本
- js获取本机操作系统类型的两种方法
- Linux操作系统添加新硬盘方法
- Javascript中获取浏览器类型和操作系统版本等客户端信息常用代码
- java如何获取本地操作系统进程列表
- Linux rdesktop操作系统下远程登录Windows XP桌面
- 32位操作系统认出超出4G内存的方法
- Linux rpm tar 操作系统下软件的安装与卸载方法
- JavaScript 获取用户客户端操作系统版本
- jsp 获取客户端的浏览器和操作系统信息
- Windows 操作系统的安全设置
- php判断当前操作系统类型
- PHP获取用户的浏览器与操作系统信息的代码
- Perl操作系统环境变量的脚本代码
- javascript获取本机操作系统类型的方法
- 封装好的js判断操作系统与浏览器代码分享
- javascript实现获取浏览器版本、操作系统类型