Atomic operation in multithreaded embedded software

别说谁变了你拦得住时间么 提交于 2019-12-02 03:40:33

Both of your questions is the same problem really.

32 bit MCU means nothing unless you disassemble the code and verify that the operation is indeed a single instruction. This is often not the case with C code.

Often you have 2 or more instructions like: "load value from stack into register", "do stuff with register", in which case it doesn't matter how many bits your MCU got. You can get an interrupt or context switch in between the two instructions.

And even if you can verify that the machine code is atomic, that's not necessarily a stable state of affairs. Make changes to the code, add more variables, link again, and suddenly code that was atomic before is atomic no longer, or vice versa.

C simply has no guarantee of atomicity. Some alternatives if you don't trust the disassembly:

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