26

It would be a very simple question (could be duplicated), but I was unable to find it.

Win32 API provides a very handy set of atomic operations (as intrinsics) such as InterlockedIncrement which emits lock add x86 code. Also, InterlockedCompareExchange is mapped to lock cmpxchg.

But, I want to do that in Linux with gcc. Since I'm working 64-bit, it's impossible to use inline assembly. Are there intrinsics for gcc?

minjang
  • 8,860
  • 9
  • 42
  • 61
  • You might want to search on "interlocked increment gcc" or "interlocked increment linux" (http://stackoverflow.com/questions/149710/interlocked-equivalent-on-linux) – D.Shawley Jan 24 '10 at 04:28

1 Answers1

32

GCC Atomic Built-ins

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • 1
    Thanks! `__sync_fetch_and_add` was the one. – minjang Jan 24 '10 at 05:22
  • 19
    Actually, the equivalent of InterlockedIncrement() would be __sync_add_and_fetch(). __sync_fetch_and_add() returns the previous value, unlike InterlockedIncrement() which returns the new value. – noamtm Nov 15 '10 at 15:09