views:

461

answers:

3

UPDATED: Is there a thread-safe, lock-free and available on all Linux distros increment function available in C or C++ ?

+3  A: 

The current C and C++ standards don't define such a thing. Your implementation may well have one.

David Seiler
+5  A: 

I think these are GNU extensions, and processor specific, but have a look at GNU C Atomic Builtins.

I think there are also atomic "libraries" available that use inline assembly etc. to provide such features.

Artelius
+5  A: 

GLib has functions to do this. You might check out http://library.gnome.org/devel/glib/stable/glib-Atomic-Operations.html

Specifically, it sounds like you want g_atomic_int_inc()

jstedfast
Would I need to include GLib as a dependency to my project? Or is this just a .lib ?
jldupont
Yes, you would need to link to glib, but that shouldn't be a problem since it is available by default on every(?) Linux distro out there.
jstedfast
It's only available on systems with GNOME installed.
HalfBrian
While technically GLib is used in GTK+ programs, and typically GTK+ programs run on GNOME, most Linux distributions I'd expect would have a copy of GLib as GTK+ programs are just so prevalent on Linux. Failing which, it's easy enough to download via the distro's package manager
blwy10