Go Down

Topic: is a read/write from/to an uint8_t atomic? (Read 2 times) previous topic - next topic

JosAH

Greetings,

the title says it all; an interrupt function decodes an IR remote signal and delivers a uint8_t value; the main control flow periodically reads this value and decides what to do; I don't mind reading an older value as long as the value isn't 'corrupted' by my interrupt handler; it can never be corrupt if the reading/writing of this variable is atomic. Is this so?

thanks in advance and kind regards,

Jos

PaulS

A change to a 8 bit variable is atomic. Changes to larger values are not. However, interrupts are disabled during an interrupt service routine, so corruption of a multibyte variable will not occur in the ISR.

The value in a multi-byte variable may change in the middle of a read/write operation outside of the ISR. That needs to protected by disabling interrupts during the read/write and then re-enabling them.

JosAH


A change to a 8 bit variable is atomic. Changes to larger values are not. However, interrupts are disabled during an interrupt service routine, so corruption of a multibyte variable will not occur in the ISR.


Thanks; that's what I wanted to know; I do enable the interrupts in my function though (reading from an IR takes several milli seconds and I want timers etc. to go on). But as long as that single byte is changed atomically, all is fine.

Thanks again for your reply and kind regards,

Jos

PeterH


I do enable the interrupts in my function though


You are enabling interrupts within an interrupt handler? That is a big red flag.
I only provide help via the forum - please do not contact me for private consultancy.

JosAH



I do enable the interrupts in my function though


You are enabling interrupts within an interrupt handler? That is a big red flag.


No it isn't, I know what I'm doing (<--- famous last words ;-) My interrupt function is triggered by a falling edge and then it starts decoding an IR signal from a digital pin; that can takes several milli seconds (I simply poll that pin in the interrupt function) and I want the millis() etc. interrupts to go on. At the start of my function I've detached it from the particular interrupt because I don't want it to be called recursively by another falling edge on that pin. b.t.w. I only enable interrupts after I've detached my function from that falling edge interrupt.

kind regards,

Jos

Go Up