Is uint32_t atomic ?

Cupcake
Posts: 4
Joined: Tue Dec 01, 2020 10:05 am

Is uint32_t atomic ?

Postby Cupcake » Wed Dec 30, 2020 9:20 am

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 ?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Is uint32_t atomic ?

Postby ESP_Angus » Mon Jan 04, 2021 9:04 am

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.

horace
Posts: 1
Joined: Mon Apr 12, 2021 6:26 am

Re: Is uint32_t atomic ?

Postby horace » 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?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Is uint32_t atomic ?

Postby ESP_Angus » Tue Apr 13, 2021 4:12 am

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.

Who is online

Users browsing this forum: axellin, Bing [Bot], snehapawar# and 102 guests