Can variables inside packed structures be read atomically?

半城伤御伤魂 提交于 2019-12-23 19:28:28

问题


I'm writing code for a Cortex M0 (ARM) CPU, and 32-bit reads/writes are atomic. Now I was wondering when I read/write 8bit/16bit variables, are they also guaranteed to be atomic? My instinct says yes, because they are internally aligned to 32-bit sections, so there is no possibility that the CPU needs two separate instructions to read/write them.

But I also happen to store a lot of variables in packed structures to save memory, and there it's possible that variables are not aligned on 32-bit boundaries, so each half of a 16-bit value could be in a different section.

So is it true that I lose atomic operations when I use packed structures?


回答1:


Using packed structures you will never have read/write atomic operations on fields that overlaps a memory unit boundary. This means that only 8bits operations are guaranteed to be atomic, otherwise it depends on the memory alignment of your fields.



来源:https://stackoverflow.com/questions/14662135/can-variables-inside-packed-structures-be-read-atomically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!