[RTT例程練習] 2.9 事件機制event

事件也是一種在線程間同步的方式。

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 << 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.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章