Force order of execution of C statements?

前端 未结 3 1655
-上瘾入骨i
-上瘾入骨i 2021-01-04 02:06

I have a problem with the MS C compiler reordering certain statements, critical in a multithreading context, at high levels of optimization. I want to know how to force ord

3条回答
  •  情歌与酒
    2021-01-04 02:42

    See _ReadWriteBarrier. That's a compiler intrinsic dedicated to what you are looking for. Be sure to check the documentation against your precise version od MSVC ("deprecated" on VS2012...). Beware cpu reordering (then see MemoryBarrier

    The documentation states that the _ReadBarrier, _WriteBarrier, and _ReadWriteBarrier compiler intrinsics (compiler reordering) and that the MemoryBarrier macro (CPU reordering) are all "deprecated" starting with VS2012. But i think they will continue to work fine for some time...

    New code may use the new C++11 facilities (links in the MSDN page)

提交回复
热议问题