Are volatile reads and writes atomic on Windows+VisualC?

后端 未结 5 2088
逝去的感伤
逝去的感伤 2020-12-14 04:49

There are a couple of questions on this site asking whether using a volatile variable for atomic / multithreaded access is possible: See here, here, or here for

5条回答
  •  猫巷女王i
    2020-12-14 05:05

    A bit off-topic, but let's have a go anyway.

    ... there are the docs for The Interlocked* functions, especially InterlockedExchange which takes a volatile(!) variable ...

    If you think about this:

    void foo(int volatile*);
    

    Does it say:

    • the argument must be a pointer to a volatile int, or
    • the argument may as well be a pointer to a volatile int?

    The latter is the correct answer, since the function can be passed both pointers to volatile and non-volatile int's.

    Hence, the fact that InterlockedExchangeX() has its argument volatile-qualified does not imply that it must operate on volatile integers only.

提交回复
热议问题