Understanding when to create tasks

ethanbowering24
Posts: 2
Joined: Sat May 25, 2024 9:00 pm

Understanding when to create tasks

Postby ethanbowering24 » Sat May 25, 2024 10:42 pm

I have an event handler attached to the IP_EVENT_STA_GOT_IP event. When it occurs, some data is exchanged over TCP and then I want the esp to run a task which periodically records and stores data from a sensor, until some other event occurs and it stops.

Should this event handler create a task which periodically records and stores data from the sensor, or should I create the task when the program starts, and then have it block itself until it's released by a task notification:

Code: Select all

ulTaskNotifyTake()
Or an event group bit being raised:

Code: Select all

xEventGroupWaitBits()
When the task should stop running, should I delete the task and recreate it again later when needed, or just block it somehow until it's needed again?

Out of curiosity, since Event Handlers are an esp-idf thing, what would you without them in vanilla FreeRTOS?
Last edited by ethanbowering24 on Sun May 26, 2024 12:28 am, edited 1 time in total.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Understanding when to create tasks

Postby ESP_Sprite » Sun May 26, 2024 12:13 am

Tasks are fairly expensive to create/destroy, so the standard way is to start the task on startup and hand them work to do via multithreading primitives like the task notifications or event groups you mentioned. Event handlers are just glorified callbacks; I imagine you'd use those as well (but e.g. hand-rolled) in a pure FreeRTOS environment; alternatively you could have e.g. a FreeRTOS queue that spits out events, but that'd require an extra task to listen to them.

ethanbowering24
Posts: 2
Joined: Sat May 25, 2024 9:00 pm

Re: Understanding when to create tasks

Postby ethanbowering24 » Sun May 26, 2024 2:14 am

ESP_Sprite wrote:
Sun May 26, 2024 12:13 am
Tasks are fairly expensive to create/destroy, so the standard way is to start the task on startup and hand them work to do via multithreading primitives like the task notifications or event groups you mentioned. Event handlers are just glorified callbacks; I imagine you'd use those as well (but e.g. hand-rolled) in a pure FreeRTOS environment; alternatively you could have e.g. a FreeRTOS queue that spits out events, but that'd require an extra task to listen to them.
Perfect, thank you so much!

Who is online

Users browsing this forum: Bing [Bot] and 66 guests