Search code examples
linuxwindowsgccatomicatomic-swap

Are Win32 InterlockedIncrement and InterlockedExchange atomic across processes?


MSDN says that the interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads.

I am not sure if they work across threads of multiple processes if the variable is in the shared memory of the processes.

Similarly what about GNU GCC compiler intrinsic: __sync_add_and_fetch and __sync_lock_test_and_set?


Solution

  • This question is essentially two questions for two different answers.

    1. For __sync_XXX builtins in GCC answer is yes.

    Refer to any online doc like this, where described, that these builtins are normally issuing full barrier, preventing even internal speculating loads inside processor pipeline. Every and all multi-thread, multi-process, etc. shared memory is safe with them.

    1. I know nothing about Windows InterLockedXXX functions. But MSDN knows, and says:

    The threads of different processes can use this mechanism if the variable is in shared memory

    So both answers are "yes".