Home > OS >  Do we need the semaphore when one task is writing to the variable and other tasks reading that varia
Do we need the semaphore when one task is writing to the variable and other tasks reading that varia

Time:06-11

I am working on freeRtos, and I have a variable called say x. Now one and only one task is writing to this variable per second and other tasks are reading the variable value. Do I need to guard the variable with mutex?

CodePudding user response:

I think it’s good practice using a mutex semaphore while changing and reading variables, used in multiple tasks.

Multibyte variables like strings could be changed while reading in another task. It will not be done every readout, but depending on the frequency the likelihood it will is much higher.

CodePudding user response:

I think you should use mutex , consider this case:(r for read pro, w for write p ro)

if r is called before w, but due to the deployment, w finishes changing the value before r reads the value. This makes a trouble, r reads the new vlaue but if expects the old value.

  • Related