RT-Thread 线程的让出
2016-05-17 11:35
211 查看
前面两个例子演示的线程调度是由系统“主动干预”的情况的线程切换,其实我们也可以根据实际情况,采用主动让出 CPU 使用权。RT-Thread 中的系统函数: rt_thread_yield(),可以让调用它的线程暂时让出 CPU 的使用权,而使下一个最高优先级的线程得以运行,但这时调用 rt_thread_yield()的线程还保持的是就绪态。这和“孔融让梨”有点像:这个梨我不吃,下一个梨我可就不客气了。
#include <rtthread.h> #include <stm32f10x.h> #include "test.h" /* 变量分配4字节对齐 */ ALIGN(RT_ALIGN_SIZE) /* 静态线程的 线程堆栈*/ static rt_uint8_t thread1_stack[512]; static rt_uint8_t thread2_stack[512]; /* 静态线程的 线程控制块 */ static struct rt_thread thread_test1; static struct rt_thread thread_test2; static void test1_thread_entry(void* parameter); static void test2_thread_entry(void* parameter); void demo_thread_creat(void) { rt_err_t result; /* 创建静态线程 : 优先级 15 ,时间片 5个系统滴答 */ result = rt_thread_init(&thread_test1, "test1", test1_thread_entry, RT_NULL, (rt_uint8_t*)&thread1_stack[0], sizeof(thread1_stack), 15, 5); if (result == RT_EOK) { rt_thread_startup(&thread_test1); } /* 创建静态线程 : 优先级 15 ,时间片 5个系统滴答 */ result = rt_thread_init(&thread_test2, "test2", test2_thread_entry, RT_NULL, (rt_uint8_t*)&thread2_stack[0], sizeof(thread2_stack), 15, 5); if (result == RT_EOK) { rt_thread_startup(&thread_test2); } } void test1_thread_entry(void* parameter) { rt_uint32_t count = 0; while (1) { /* 打印线程 1 的输出 */ rt_kprintf("thread1: count = %d\n", count ++); /* 执行 yield 后应该切换到 test2 执行 */ rt_thread_yield(); } } void test2_thread_entry(void* parameter) { rt_uint32_t count = 0; while (1) { /* 执行 yield 后应该切换到 test1 执行 */ rt_thread_yield(); /* 打印线程 2 的输出 */ rt_kprintf("thread2: count = %d\n", count ++); } }
相关文章推荐
- 强烈推荐android studio用的几个插件
- 大数据Java基础第九天作业
- Cocos2d-x 3.x 全平台新手开发配置教程
- Android 彻底征服 ListView 四 QQ微信 聊天ListView
- 买卖股票的最佳时机
- 版本控制工具SVN使用详细解析
- HTTP响应头和请求头信息对照表
- CentOS6.5 安装Kafka集群
- sync_binlog innodb_flush_log_at_trx_commit 浅析 传
- php 利用远程png图片创建新png图片
- 使用CSS3美化HTML表单的技巧演示
- Android Dialog全屏
- 如何从Eclipse导入github上的项目源码
- MySQL基础1——Express与MySQL联调.md
- 使用Python访问restAPI接口
- Android蓝牙完全学习手册
- linux C之access函数
- Maven入门指南③:坐标和依赖
- Java小程序:输入字母和数字,输出时字母在前数字在后
- 手指点击的地方随机生成带运动的不同颜色不同大小的圆