0

I got 4 threads running on ESP32 (dual core):

  • 1 thread running on 1st core which is related to the WiFi.
  • 1 thread running on 2nd core which is related to the touchscreen.
  • 1 thread running on 2nd core which is related to the display.
  • 1 thread running on 2nd core which is related to the sensors.

Some of the threads execute almost instantly, while for instance the one handling the sensors can take a few seconds per interaction, and no blocking should occur meanwhile for the rest of the threads.

Anyway, with the growing of the program, a lot of variables started to be involved in data transfers between the cores, and since the whole thing is about to be redone, I have the following questions, to clear the things up for myself, since I found quite a lot of confusing, even contradictory information in examples and explanations on the internet:

  • Is the "volatile" keyword really sufficient for exchange of data between threads (no ISR)?
  • Should "volatile" always be between "noInterrupts()" and "interrupts()", even when there is no ISR function?
  • What approaches should be used when exchanging a lot of variables between the threads (currently about 40 variables, but the number will probably grow)?
  • Is xQueueSend / xQueueReceive blocking in case it is put all around the threads with different variables?
1
2

Is the "volatile" keyword really sufficient for exchange of data between threads (no ISR)?

No - "volatile" just tells the compiler that the variable may change value due to something other than the immediate code being generated and informs the code-generator not to rely on the value remaining static when it would otherwise expect so. When copying the value to a register for instance, it would normally expect that register value to continue to represent the value of the variable in memory but if the variable is volatile, that assumption doesn't hold. This would be used to indicate that an ISR might change it at any time; that the variable is an input register and may be changed from the outside at any time; or that another thread may change it, again, at any time (assuming pre-emptive multi-threading or multi-tasking).

Should "volatile" always be between "noInterrupts()" and "interrupts()", even when there is no ISR function?

No - volatile just describes an attribute about the variable over time

What approaches should be used when exchanging a lot of variables between the threads

Use a semaphore here. A thread that wants access to one of - or the collection of - shared variables asks for the semaphore. The RTOS either returns it immediately, or (because another thread already holds it) suspends the task until it becomes available, and returns it then. The thread holding the semaphore may access the variables (or whatever shared resource this semaphore controls), then returns the semaphore on completion. Semaphores are usually built in to the RTOS.

(As I'm not familiar with your particular RTOS, I'll leave your fourth question for someone who is).

1
  • Thank You for the points JRobert. I am using FreeRTOS ( Espriff IDF ) – Yordan Yanakiev May 8 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.