How to guarantee 64-bit writes are atomic?

前端 未结 8 1315
不知归路
不知归路 2020-12-23 22:34

When can 64-bit writes be guaranteed to be atomic, when programming in C on an Intel x86-based platform (in particular, an Intel-based Mac running MacOSX 10.4 using the Inte

8条回答
  •  臣服心动
    2020-12-23 23:18

    If you want to do something like this for interthread or interprocess communication, then you need to have more than just an atomic read/write guarantee. In your example, it appears that you want the values written to indicate that some work is in progress and/or has been completed. You will need to do several things, not all of which are portable, to ensure that the compiler has done things in the order you want them done (the volatile keyword may help to a certain extent) and that memory is consistent. Modern processors and caches can perform work out of order unbeknownst to the compiler, so you really need some platform support (ie., locks or platform-specific interlocked APIs) to do what it appears you want to do.

    "Memory fence" or "memory barrier" are terms you may want to research.

提交回复
热议问题