2

I am new to real time programming and I am trying to practice.

In the example I am working on : Task1 must change a variable called frequency periodically and task2 will blink led each time with the new frequency.

Should I use mutex on a shared variable "Freq" or use a queue to send periodically the frequency from task 1 and task 2.

Mohamed
  • 57
  • 1
  • 9

1 Answers1

2

If only one task is changing the variable, and all other tasks (just one in your case) is reading the variable, then you don't need to protect the variable at all provided the variable can be written in one go. So if this is a 32-bit architecture and the variable is 32-bits, then no protection is needed. If on the other hand it was an 8-bit architecture and the variable was 32-bits then it would take 4 writes to update all 32-bits and the variable would need protecting. This is only true when there is only one writer - if more than one task was writing to the variable then it would need protecting.

However, just updating the variable does not signal to the reading task that the variable has changed. For efficiency you might only want the reading task to execute when the variable changes, in which case you could send the updated variable on a queue and have the reading task blocked on the queue and automatically unblocked when there is data in the queue (https://www.freertos.org/Embedded-RTOS-Queues.html) - depending on the frequency of update though it might be more efficient to send the updated variable directly to the task as a direct to task notification, and have the reading task blocked on the notification (https://www.freertos.org/RTOS-task-notifications.html).

Richard
  • 3,081
  • 11
  • 9