How to use multiple flags for intra-thread signaling?

Hello Li,

How does this work? Each flag use one bit of the 32 bit parameter? Or some combination bits?

Each flag is using one of the last 31 bits of of the EventFlags variable. The most significant is not used.

  • I usually first define the flag’s position within the EventFlags variable, for example:
#define MOTOR_FLAG        (1UL << 0)    // position 0
#define MAX_SPEED_FLAG    (1UL << 1)    // position 1
#define RELAY_ON_FLAG     (1UL << 8)    // position 8
...
  • Then I can set the selected flag(s) in the program like:
EventFlags evenFlags;

...
evenFlags.set(MAX_SPEED_FLAG);
...
evenFlags.set(MOTOR_FLAG | MAX_SPEED_FLAG)
...
  • After that I can wait and block some thread until the selected flag(s) become(s) set:
...
eventFlags.wait_any(MOTOR_FLAG | RELAY_ON_FLAG);
...
eventFlags.wait_all(MOTOR_FLAG | MAX_SPEED_FLAG);
...
  • By default the wait function clears the flags passed to it. If you don’t want the wait function to clear the flag(s) then pass it a second argument defining the wait time in millisecond and third one equal to false:
eventFlags.wait_all(MOTOR_FLAG | MAX_SPEED_FLAG,  osWaitForever, false);
  • Below is an example how to use them in program:
#include "mbed.h"

#define SAMPLE_FLAG1    (1UL << 0)
#define SAMPLE_FLAG2    (1UL << 1)

DigitalOut      led1(LED1, 0);
DigitalOut      led2(LED2, 0);

EventFlags      event_flags;
osThreadId_t    threadId;

uint32_t    flags_read = 0;
Thread      thread_01;
Thread      thread_02;

void task_01()
{
    while (true) {
        led1 = 1;
        event_flags.set(SAMPLE_FLAG1);
        ThisThread::sleep_for(500ms);
        led1 = 0;
        event_flags.set(SAMPLE_FLAG2);
        ThisThread::sleep_for(500ms);
    }
}

void task_02()
{
    while (true) {
        event_flags.wait_all(SAMPLE_FLAG1);
        led2 = 1;
        event_flags.wait_all(SAMPLE_FLAG2);
        led2 = 0;
    }
}

int main()
{
    event_flags.clear();
    thread_01.start(callback(task_01));
    thread_02.start(callback(task_02));

    printf("Waiting for any flag.\r\n");

    while (true) {
        flags_read = event_flags.wait_any(SAMPLE_FLAG1 | SAMPLE_FLAG2, osWaitForever, true);
        printf("Got: 0x%.04x\r\n", flags_read);
        printf("led1 = %d\r\n", led1.read());
        printf("led2 = %d\r\n", led2.read());
    }
}
4 Likes