一、事件集
- 事件:唤醒线程的条件。
- 与信号量的区别:
- 信号量主要用于一对一的线程同步;当需要“一对多”,“多对一”,“多对多”时就需要事件集处理。
- 线程通过访问set中的标志位,结合逻辑或(独立型)、与(关联型)建立事件组合,控制线程是否要发生。
- 事件集控制块
/* * event structure */ struct rt_event { struct rt_ipc_object parent; /**< inherit from ipc_object */ rt_uint32_t set; /**< event set */ }; typedef struct rt_event *rt_event_t;
通过32位无符号整形set来为事件集是否发生做标记。
- 事件的发送和接受
//发送事件 rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set); /** * This function will receive an event from event object, if the event is * unavailable, the thread shall wait for a specified time. * * @param event the fast event object * @param set the interested event set 写入对什么位置可以进行修改, 比如 set=0x01|0x08说明调用这个API的线程对第0、3个线程感兴趣。 * @param option the receive option, either RT_EVENT_FLAG_AND(关联型) or * RT_EVENT_FLAG_OR(独立型) should be set.事件信息标记 RT_EVENT_FLAG_CLEAR 接收完后清除事件标志 * @param timeout the waiting time * @param recved the received event, if you don't care, RT_NULL can be set. 用于保存接受到的事件。 * * @return the error code */ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t opt, rt_int32_t timeout, rt_uint32_t *recved);
- 与信号量的区别:
-
不论是信号量、互斥量、事件集,如果take或者receive不到线程都会挂起。