Synchronization between multiple tasks


I have an application which makes use of the following components:

  • OTA functionality
  • MQTT
  • HTTP server
  • HTTP client

In addition, the application may start three data producing tasks, depending on client configuration: Modbus RTU, Modbus TCP and UART.

I am not sure how to implement synchronization between the data producing tasks. One problem I’ve faced is that the Modbus serial taks experiences timeouts if other tasks (WiFi for example) are preempting it. My questions are:

  • How can I implement synchronization between the producer tasks without knowing in advance if all of them will run? Using xEventGroupSync() looks like an option, but how can I use it without knowing which tasks will run?
  • How to ensure tasks run in order (1st, 2nd, 3rd and so on)
  • How to stop these tasks during OTA


@sofronii A queue for fixed length data or Message Buffer for variable length data would be a reasonable way to transfer data from producer tasks to the consumer task.

You might consider a Stream Buffer between your UART receive handler and Modbus RTU task, since uart receive is particularly time-sensitive.

If you need calls to run in a particular order, you might only need one task rather than multiple tasks. Without a better understanding of your application, it is hard to recommend a particular strategy.

Be sure to check your wifi and uart drivers for busy-waits that are not using a FreeRTOS primitive (semaphore, mutex, task notification, queue), since these can often lead to wasted cpu time.

An interrupt or DMA driven approach to your uart driver is probably a good choice for your use case.

1 Like