Atomic

Description The following functions can be used to atomically access integers and pointers. They are implemented as inline assembler function on most platforms and use slower fall-backs otherwise. Using them can sometimes save you from using a performance-expensive GMutex to protect the integer or pointer. The most important usage is reference counting. Using g_atomic_int_inc() and g_atomic_int_dec_and_test() makes reference counting a very fast operation. Note You must not directly read integers or pointers concurrently accessed by multiple threads, but use the atomic accessor functions instead. That is, always use g_atomic_int_get() and g_atomic_pointer_get() for read outs. They provide the neccessary synchonization mechanisms like memory barriers to access memory locations concurrently. Note If you are using those functions for anything apart from simple reference counting, you should really be aware of the implications of doing that. There are literally thousands of ways to shoot yourself in the foot. So if in doubt, use a GMutex. If you don't know, what memory barriers are, do not use anything but g_atomic_int_inc() and g_atomic_int_dec_and_test(). Note It is not safe to set an integer or pointer just by assigning to it, when it is concurrently accessed by other threads with the following functions. Use g_atomic_int_compare_and_exchange() or g_atomic_pointer_compare_and_exchange() respectively.

Members

Static functions

intAdd
void intAdd(int* atomic, int val)

Atomically adds val to the integer pointed to by atomic. Also acts as a memory barrier. Since 2.4

intCompareAndExchange
int intCompareAndExchange(int* atomic, int oldval, int newval)

Compares oldval with the integer pointed to by atomic and if they are equal, atomically exchanges *atomic with newval. Also acts as a memory barrier. Since 2.4

intDecAndTest
int intDecAndTest(int* atomic)

Atomically decrements the integer pointed to by atomic by 1. Since 2.4

intExchangeAndAdd
int intExchangeAndAdd(int* atomic, int val)

Atomically adds val to the integer pointed to by atomic. It returns the value of *atomic just before the addition took place. Also acts as a memory barrier. Since 2.4

intGet
int intGet(int* atomic)

Reads the value of the integer pointed to by atomic. Also acts as a memory barrier. Since 2.4

intInc
void intInc(int* atomic)

Atomically increments the integer pointed to by atomic by 1. Since 2.4

intSet
void intSet(int* atomic, int newval)

Sets the value of the integer pointed to by atomic. Also acts as a memory barrier. Since 2.10

pointerCompareAndExchange
int pointerCompareAndExchange(void** atomic, void* oldval, void* newval)

Compares oldval with the pointer pointed to by atomic and if they are equal, atomically exchanges *atomic with newval. Also acts as a memory barrier. Since 2.4

pointerGet
void* pointerGet(void** atomic)

Reads the value of the pointer pointed to by atomic. Also acts as a memory barrier. Since 2.4

pointerSet
void pointerSet(void** atomic, void* newval)

Sets the value of the pointer pointed to by atomic. Also acts as a memory barrier. Since 2.10

Meta