Page 1 of 1

Global boolean vs EventGroup bit

Posted: Thu Jul 12, 2018 11:50 pm
by 0xffff
I'm curious about the benefits of using EventGroup bits for event notification (not task synchronization) compared to a global. For example, task A can set a global boolean, which is checked by task B in a loop with task Delay:

Code: Select all


// task A sets the marker
bool marker;
function setMarker() {
   marker = true;
}

// Task B checks the marker every second
while ( !marker ) {
    vTaskDelay(1000);
};

compared to the following pattern:

Code: Select all


// task A sets the marker
extern EventGroupHandle_t events;
function setMarker() {
   xEventGroupSetBits(events, 1);
}

// Task B blocks until this bit i set
xEventGroupWaitBits(events,1,pdFALSE,pdFALSE,portMAX_DELAY)

Presumably the second version will return as soon as the bit is set so there could be a lower latency to detection. Are there other benefits?

TIA

Re: Global boolean vs EventGroup bit

Posted: Fri Jul 13, 2018 12:36 am
by ESP_Angus
Lower latency, and also the waiting task will not run at all until the event group wakes it up - so it's not using any CPU time.

Re: Global boolean vs EventGroup bit

Posted: Fri Jul 13, 2018 12:52 am
by urbanze
1. Block time is the best reason (I think), leaving task blocked with 0 cpu consuption (quoted above).

2. One boolean use 8bit, than, to use 24 events (1 event group), you need much more memory.

Re: Global boolean vs EventGroup bit

Posted: Fri Jul 13, 2018 8:39 am
by chegewara
You guys are forgetting about few important things about EventGroup:
  • WaitForBits function got timeout parameter, so code can run even if "boolean" value is still false (only if you want to),
  • WaitForBits can wait for few bits to be set, with bool you have to use few global variables instead, this makes code less ram consuming

Re: Global boolean vs EventGroup bit

Posted: Fri Jul 13, 2018 10:59 am
by urbanze
and dont forget event group special spec, ALL tasks waiting for X bit, will wake up when X bit arrive (leave blocked state to ready). Queue and Semaphr wake up only most priority task or the oldest (if tasks has same prio).