0

I am familiar with the race condition and the famous counter++ example. My question is not about read-write protection, but only about read protection. Is it possible I read a corrupt value via an interrupt? incase another interrupt/main code was just in the process of changing its value? Do I need to protect my reads of let's say volatile components? I am using C and an ARM microcontroller.

Furthermore, if only one task is doing read-write manipulation on some variable, and all other tasks are only reading it, do I need to protect it?

schanti schul
  • 681
  • 3
  • 14
  • What reads what from where? The ISR reads a variable shared with background program, or...? I don't understand the question, please make a simple pseudo code example to illustrate. – Lundin May 08 '19 at 07:28
  • 3
    Here's some info regarding use of volatile with ISRs, also addressing the separate problem of race conditions: https://electronics.stackexchange.com/a/409570/6102 – Lundin May 08 '19 at 07:31
  • There are specific arm documents on this. For instance an aligned 32 bit is always ok. However, 64 bits could take several bus cycles and be interrupted mid-cycle as a hypothetical; consult your arm arm for real details. – artless noise May 08 '19 at 12:41
  • A similar question and answers on [how to protect a global variable shared by isr and regular function](https://stackoverflow.com/questions/18088873/how-to-protect-a-global-variable-shared-by-isr-and-regular-function). [sig_atomic_t](https://stackoverflow.com/questions/24931456/how-does-sig-atomic-t-actually-work) is some what related. – artless noise May 08 '19 at 13:16

1 Answers1

0

Here is my approach:

do
{
   Read_Flag = 0;
   //Read operation
} while (Read_Flag);

Read_Flag to be set at ISR where value of a variable being read is modified.

Babajan
  • 364
  • 3
  • 5