Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » semaphores and taskmanagerio

Author: werner
01/04/2022 12:35:57
Hi,
I use TaskmanagerIO with esp32.
How can I include semaphores with taskmanagerio ?
I have read something about xSemaphoreCreateBinary();
But is this the right way ?

best regards

Werner

Author: davetcc
03/04/2022 07:53:54
You can use semaphores and mutexes with TaskManagerIO but you must be very careful that you don't lock the event processor as it is intentionally single-threaded. Locking or waiting for a resource that is not heavily contended is fine, but if the resource is often locked it may be better to do the locking asynchronously outside of task manager and post the results back in.

In the event that latency would be introduced, there are many ways in the past we have tried or thought of to move the work out of task manager:

* Using a second task manager as a queue on another thread where you just submit work to it for immediate execution using the execute method. This second instance of task manager acts like a queue and posts finished events back to the main task manager. This works really well for tasks that introduce a lot of latency.
* Using the circular buffer support in SimpleCollections library and have a thread constantly reading the buffer. https://github.com/davetcc/SimpleCollections
* Using an ESP32 task that posts the results into task manager.

EDIT - with any of the above solutions, you're opening yourself up to access from multiple threads, so the usual rules about atomicity and happens before - happens after apply. IE variable access must be atomic and critical blocks of code must be protected from multiple threads accessing at once.




Register / Login  |  Desktop view  |  Jump to top of page