it is said access 32bit variable on 32bit architecure mcu or access 16bit variable on 16bit architecure,both are atomic.
suppose I want access memeber of structure, like
a_struct.var1 = 1;
b_struct.var3 = 3;
b_struct.var4 = 4;
suppose I use 32bit MCU,Is above opration atomic? or I do need use mutex or critical section or lock scheduler.
It’s still a native 32/16 bit access and should be ok. Organizing variables in a
struct doesn’t matter.
It might be different for special cases where a struct is not natively aligned or if it’s tagged with a non-default alignment attribute (e.g.
packed) or if members are tagged individually with non-default alignment attributes. In this cases it depends on the binary layout of the struct and/or the capabilities of the MCU.
And it’s good style to format code as code in posts (using the
</> button) for better readabilty
and again,for your notice.
I will mention that most 32 bit processors have instructions that can access 8 or 16 bit locations atomically, but there can be some that don’t, (or with options like packed, the compiler doesn’t use them).
One other thing to point out, since the structures or members weren’t declared volatile, the compiler is allowed to reorder the writing to the various members if it wants to. Also, while each individual write will be atomic (i.e. you will always read either the old value or the new value, never some mix of the two), it is NOT atomic as a set, another task might see the first members updated and later members not, so the members were updated atomically, but the structures are not.