Global boolean vs EventGroup bit

0xffff
Posts: 25
Joined: Tue Jun 19, 2018 1:53 am

Global boolean vs EventGroup bit

Postby 0xffff » Thu Jul 12, 2018 11:50 pm

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

ESP_Angus
Posts: 1170
Joined: Sun May 08, 2016 4:11 am

Re: Global boolean vs EventGroup bit

Postby ESP_Angus » Fri Jul 13, 2018 12:36 am

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.

User avatar
urbanze
Posts: 196
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Global boolean vs EventGroup bit

Postby urbanze » Fri Jul 13, 2018 12:52 am

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.

chegewara
Posts: 436
Joined: Wed Jun 14, 2017 9:00 pm

Re: Global boolean vs EventGroup bit

Postby chegewara » Fri Jul 13, 2018 8:39 am

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

User avatar
urbanze
Posts: 196
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Global boolean vs EventGroup bit

Postby urbanze » Fri Jul 13, 2018 10:59 am

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

Who is online

Users browsing this forum: No registered users and 4 guests