Page 1 of 1
Is uint32_t atomic ?
Posted: Wed Dec 30, 2020 9:20 am
by Cupcake
Hi,
I would like to know if when we store a value in uint32_t variable it's an atomic operation or not with esp32 ?
How to know which variable type is atomic and which not ?
Re: Is uint32_t atomic ?
Posted: Mon Jan 04, 2021 9:04 am
by ESP_Angus
Hi Cupcake,
Reads and writes will be atomic (the compiler generates 32-bit load and store operations.)
Updating (read/modify/write) is not atomic. You can either use a FreeRTOS "portmux" and critical section to protect access to the variable, or use the standard C atomic API in stdatomic.h to perform an atomic update operation.
Re: Is uint32_t atomic ?
Posted: Mon Apr 12, 2021 6:29 am
by horace
Can you confirm if this is the case for memory used by threads on different cores?
If each read/write is atomic, does this apply when each core reads or writes simultaneously?
Re: Is uint32_t atomic ?
Posted: Tue Apr 13, 2021 4:12 am
by ESP_Angus
horace wrote: ↑Mon Apr 12, 2021 6:29 am
Can you confirm if this is the case for memory used by threads on different cores?
If each read/write is atomic, does this apply when each core reads or writes simultaneously?
Yes, it does. If both cores write to the same 32-bit variable, the value should hold either the value written by one core or the other, not a mix of the two values.
Similarly, if one core reads while the core writes then it should see either the old 32-bit value or the new 32-bit value, not a mix of the two.