By the use of Spinlocks, it is possible to serialize blocks of code that might run parallel on multiple CPU cores.
More...
By the use of Spinlocks, it is possible to serialize blocks of code that might run parallel on multiple CPU cores.
Synchronization is implemented using a lock variable. Once a thread enters the critical area, it sets the lock variable (to a non-zero value); when this thread leaves the critical area, it resets the lock variable to zero. When trying to enter an already locked critical area, the trying thread actively waits until the critical area is free again.
Use the following two GCC intrinsics
bool __atomic_test_and_set(void *ptr, int memorder)
void __atomic_clear (bool *ptr, int memorder)
These intrinsics are translated into atomic, architecture-specific CPU instructions.
- Note
- If you want that things just work, choose
__ATOMIC_SEQ_CST
as memorder. This is not the most efficient memory order but works reasonably well.
Atomic Builtins in GCC manual