tags:

views:

265

answers:

5

Are there functions for performing atomic operations (like increment / decrement of an integer) etc supported by C Run time library or any other utility libraries?

If yes, what all operations can be made atomic using such functions?

Will it be more beneficial to use such functions than the normal synchronization primitives like mutex etc?

OS : Windows, Linux, Solaris & VxWorks

+1  A: 

'Beneficial' is situational. Always, performance depends on circumstances. You may expect something wonderful to happen when you switch out a mutex for something like this, but you may get no benefit (if it's not that popular of a case) or make things worse (if you accidently create a 'spin-lock').

bmargulies
A: 

Not sure what you mean by the C runtime library. The language proper, or the standard library does not provide you with any means to do this. You'd need to use a OS specific library/API. Also, don't be fooled by sig_atomic_t -- they are not what it seems at first glance and are useful only in the context of signal handlers.

dirkgently
A: 

On Windows, there are InterlockedExchange and the like. For Linux, you can take glibc's atomic macros - they're portable (see i486 atomic.h). I don't know a solution for the other operating systems.

In general, you can use the xchg instruction on x86 for atomic operations (works on Dual Core CPUs, too).

As to your second question, no, I don't think that using atomic operations will be faster than using mutexes. For instance, the pthreads library already implements mutexes with atomic operations, which is very fast.

AndiDog
+3  A: 

The C library doesn't have any.

On Linux, gcc provides some -- look for _synch_fetch_and_add, _synch_fetch_and_sub, and so on.

In the case of Windows, look for InterlockedIncrement, InterlockedDecrement, InterlockedExchange, and so on. If you use gcc on Windows, I'd guess it also has the same built-ins as it does on Linux (though I haven't verified that).

On Solaris, it'll depend. Presumably if you use gcc, it'll probably (again) have the same built-ins it does under Linux. Otherwise, there are libraries floating around, but nothing really standardized.

Jerry Coffin
_synch_fetch_and_* works pretty well and portably across anything that can host gcc, except for ARM. +1
Tim Post
A: 

Across all supported platforms, you can use use GLib's atomic operations. On platforms which have atomic operations built-in (e.g. assembly instructions), glib will use them. On other platforms, it will fall back to using mutexes.

I think that atomic operations can give you a speed boost, even if mutexes are implemented using them. With the mutex, you will have at least two atomic ops (lock & unlock), plus the actual operation. If the atomic op is available, it's a single operation.

Michael E