Posix Equivalent for InterlockedExchange

Viewed 70

What is the Posix equivalent to the Windows InterlockedExchange?

I have found the equivalent for InterlockedCompareExchange (i.e. __sync_val_compare_and_swap) but not simple exchange. I found this answer that gave a solution in assembly, but I need a cross platform/architecture C solution.

1 Answers

If you have access to C2011 stdatomic.h, then I believe atomic_exchange is what you are looking for.

I am not aware of any other standardized equivalent. In particular, the current version of POSIX does not mandate the presence of stdatomic.h (which is optional in C2011), and no version of POSIX has defined its own atomic operations, only higher-level thread synchronization operations (pthread_mutex_* for instakce).

Related