What Rules does compiler have to follow when dealing with volatile memory locations?

前端 未结 5 783
故里飘歌
故里飘歌 2020-12-03 05:34

I know when reading from a location of memory which is written to by several threads or processes the volatile keyword should be used for that location like

5条回答
  •  情书的邮戳
    2020-12-03 06:16

    The compiler is not allowed to optimize away reads of a volatile object in a loop, which otherwise it'd normally do (i.e. strlen()).

    It's commonly used in embedded programming when reading a hardware registry at a fixed address, and that value may change unexpectedly. (In contrast with "normal" memory, that doesn't change unless written to by the program itself...)

    That is it's main purpose.

    It could also be used to make sure one thread see the change in a value written by another, but it in no way guarantees atomicity when reading/writing to said object.

提交回复
热议问题