当前位置:Gxlcms > mysql > [RTT例程练习]2.9事件机制event

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

时间:2021-07-01 10:21:17 帮助过:26人阅读

事件也是一种在线程间同步的方式。 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 
#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 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.

人气教程排行