Visual C++ Volatile

前端 未结 1 815
孤街浪徒
孤街浪徒 2021-01-12 03:43

The MSDN docs for \"volatile\" in Visual C++ indicate that writes have \"release semantics\" and that reads have \"acquire semantics\", in addition to ensuring that reads al

1条回答
  •  囚心锁ツ
    2021-01-12 04:13

    Is there any way in Visual C++ to get the "C" volatile behaviour only, without the memory fence?

    On x86 there are no memory fences created at the assembly level on reads and writes to a volatile memory location since on that platform every load has acquire semantics, and every store has release semantics. Therefore for MSVC on x86, the volatile directive simply directs the compiler to prevent the reordering of loads and stores depending on if you are writing or reading from the memory location that was marked volatile.

    You would only incur the "penalty" of a memory fence on the IA64 architecture, since there the memory ordering model of the platform does not ensure acquire and release semantics for loads and stores.

    Keep in mind this behavior is MSVC-specific, and is not a standardized semantic of volatile.

    Update: According to @ildjarn you would also see a memory fence on ARM with Windows 8 since that platform also has a weakly ordered memory-consistency model like IA64.

    0 讨论(0)
提交回复
热议问题