Menu

#31 [threads] improving atomic ops.

open
nobody
None
5
2009-02-01
2009-02-01
No

the current loki uses for AtomicIncrement/Decrement winapi critical sections or pthread mutexes.
this can be done faster:
- with winapi: InterlockedIncrement/Decrement
- with gcc: __sync_add/sub_and_fetch ) builtins that are translated into e.g. xadd opcode on x86.

#ifdef _MSC_VER
#include <windows.h>
#endif

long increment( long& counter )
{
return ::InterlockedIncrement( &counter );
}

long decrement( long& counter )
{
return ::InterlockedDecrement( &counter );
}

#elif ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ >= 1 )

long increment( long& counter )
{
return __sync_add_and_fetch( &counter, 1 );
}

long decrement( long& counter )
{
return __sync_sub_and_fetch( &counter, 1 );
}

#else

// legacy implementation with mutextes.

#endif

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.