博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RTT例程练习] 2.9 事件机制event
阅读量:4704 次
发布时间:2019-06-10

本文共 3107 字,大约阅读时间需要 10 分钟。

事件也是一种在线程间同步的方式。

RTT中,事件是一个32bit(4个字节)的变量,其中每一个位可以表示代表一种事件。接收事件的线程既可以在多个事件同时发生后(即多个bit位同时置1)触发,正如本例中线程1中第一条语句所演示的那样。也可以多个事件任意一个发生后(即多个bit位任意一个置位)就可以触发。主程序中创建三个线程,线程1接收事件标志。线程2和线程3则向发送事件标志。可以说,事件更为灵活。

这里,一共创建了三个线程,一个事件。一开始 thread1 等待事件 bit3 和 bit5 发生,并且是 and 方式。thread2 和 thread3 分别发送了bit3 和 bit5 ,thread1收到之后,等待1s 进入下一次等待事件的发生。第二次事件是 or 的方式,即 bit3 和 bit5 只要有一个方式,事件就算发生,这时 thread3 发出 bit5 事件,thread1 收到后停止调度。

程序:

#include 
#include
void rt_init_thread_entry(void *parameter){}static struct rt_event event;static rt_uint8_t thread1_stack[1024];struct rt_thread thread1;static void thread1_entry(void *parameter){ rt_uint32_t e; if (rt_event_recv(&event, ((1 << 3) | (1 << 5)), RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e) == RT_EOK) { rt_kprintf("thread1: AND recv event 0x%x\n", e); } rt_kprintf("thread1: delay 1s to prepare second event.\n"); rt_thread_delay(RT_TICK_PER_SECOND); if (rt_event_recv(&event, ((1 << 3) | (1 << 5)), RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e) == RT_EOK) { rt_kprintf("thread1: OR recv event 0x%x\n", e); } rt_kprintf("thread1 leave.\n"); }static rt_uint8_t thread2_stack[1024];struct rt_thread thread2;static void thread2_entry(void *param){ rt_kprintf("thread2: send event1\n"); rt_event_send(&event, (1 << 3)); rt_kprintf("thread2 leave.\n");}static rt_uint8_t thread3_stack[1024];struct rt_thread thread3;static void thread3_entry(void *param){ rt_kprintf("thread3: send event2\n"); rt_event_send(&event, (1 << 5)); rt_thread_delay(20); rt_kprintf("thread3: send event2\n"); rt_event_send(&event, (1 << 5)); rt_kprintf("thread3 leave.\n");}int rt_application_init(){ rt_thread_t init_thread; rt_err_t result; result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO); if (result != RT_EOK) { rt_kprintf("init event failed.\n"); return -1; } init_thread = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 2048, 7, 20); if (init_thread != RT_NULL) rt_thread_startup(init_thread); rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 8, 50); rt_thread_startup(&thread1); rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack),9,50); rt_thread_startup(&thread2); rt_thread_init(&thread3, "thread3", thread3_entry, RT_NULL, &thread3_stack[0], sizeof(thread3_stack),10,50); rt_thread_startup(&thread3); return 0;}
结果:

thread2: send event1thread2 leave.thread3: send event2thread1: AND recv event 0x28thread1: delay 1s to prepare second eventthread3: send event2thread3 leave.thread1: OR recv event 0x20thread1 leave.

转载于:https://www.cnblogs.com/lyyyuna/archive/2013/04/01/4123921.html

你可能感兴趣的文章
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
arrow:让Python的日期与时间变的更好
查看>>
(转)Excel的 OleDb 连接串的格式(连接Excel 2003-2013)
查看>>
Java并发编程
查看>>