Home  >  Article  >  Database  >  [RTT例程练习] 2.9 事件机制event

[RTT例程练习] 2.9 事件机制event

WBOY
WBOYOriginal
2016-06-07 15:17:461155browse

事件也是一种在线程间同步的方式。 RTT中,事件是一个32bit(4个字节)的变量,其中每一个位可以表示代表一种事件。接收事件的线程既可以在多个事件同时发生后(即多个bit位同时置1)触发,正如本例中线程1中第一条语句所演示的那样。也可以多个事件任意一个

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

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 <rtthread.h>
#include <string.h>
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 结果:
<pre class="brush:php;toolbar:false">thread2: send event1
thread2 leave.
thread3: send event2
thread1: AND recv event 0x28
thread1: delay 1s to prepare second event
thread3: send event2
thread3 leave.
thread1: OR recv event 0x20
thread1 leave.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn