UNIX Portable Atomic Operations

前端 未结 7 1397
生来不讨喜
生来不讨喜 2020-12-05 07:01

Is there a (POSIX-)portable way in C for atomic variable operations similar to a portable threading with pthread?

Atomic operations are operations like \"increment a

7条回答
  •  孤街浪徒
    2020-12-05 07:35

    Since you asked for OS X:

    (and since cross platformity was raised in this thread.)

    OS X has functions OSAtomicAdd32() and friends. They are declared in "/usr/include/libkern/OSAtomic.h". See The Threading Programming guide, section "Using Atomic Operations".

    And for Windows, there is InterlockedIncrement() and friends (see MSDN).

    Together with the gcc builtins __sync_fetch_and_add() and friends (has been linked above), you should have something for every main desktop platform.

    Please note that I did not yet use them by myself, but maybe will do so in the next few days.

提交回复
热议问题