简体   繁体   中英

Azure RTOS how to signal multiples thread with single event?

I want to signal multiple threads with one event, what is the best way to do this? For example: Whenever 1 second passes, RTC interrupt occurs and it sets its related event flag, after that whichever thread waits for this event, starts running, and does its job.

RTC interrupt:    
    tx_event_flags_set (&rtc_events, 1_SEC_PASS, TX_OR);

1st Thread: 
    tx_event_flags_get (&rtc_events, 1_SEC_PASS, TX_OR_CLEAR, &flag_val, TX_WAIT_FOREVER);

2nd Thread: 
    tx_event_flags_get (&rtc_events, 1_SEC_PASS, TX_OR_CLEAR, &flag_val, TX_WAIT_FOREVER);

If I use it like that, only one of the threads will be notified. I can use multiple event flags for the same RTC event, but this time RTC would be dependent on the threads and this will be bad in terms of loose coupling.

RTC interrupt:    
    tx_event_flags_set (&first_thread_events, 1_SEC_PASS, TX_OR);
    tx_event_flags_set (&second_thread_events, 1_SEC_PASS, TX_OR);

1st Thread: 
    tx_event_flags_get (&first_thread_events, 1_SEC_PASS, TX_OR_CLEAR, &flag_val, TX_WAIT_FOREVER);

2nd Thread: 
    tx_event_flags_get (&second_thread_events, 1_SEC_PASS, TX_OR_CLEAR, &flag_val, TX_WAIT_FOREVER);

Which of these methods would be best? Is there any third way to solve this problem, in terms of better software design? Or am I worrying too much?

Please give me some advice.

The event flags are a pretty basic concept and are not really suitable for notifying multiple threads (such as your first example).

Your second example is the correct way to go, you want a flag for each thread that is to be updated.

If your application requires further decoupling, then you would be would likely want to move to some form of publish/subscribe model, where they threads can explicitly subscribe to the messages they are interested in, and the RTC thread would publish the message.

Ryan is correct. You can also have multiple flags within one group, ie you do not need to create 2 groups like you do in your second example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM