Does the C++ volatile keyword introduce a memory fence?

前端 未结 13 2203
一整个雨季
一整个雨季 2020-11-28 20:40

I understand that volatile informs the compiler that the value may be changed, but in order to accomplish this functionality, does the compiler need to introduc

13条回答
  •  一个人的身影
    2020-11-28 21:10

    The compiler needs to introduce a memory fence around volatile accesses if, and only if, that is necessary to make the uses for volatile specified in the standard work (setjmp, signal handlers, and so on) on that particular platform.

    Note that some compilers do go way beyond what's required by the C++ standard in order to make volatile more powerful or useful on those platforms. Portable code shouldn't rely on volatile to do anything beyond what's specified in the C++ standard.

提交回复
热议问题