template<typename T, template< class > class spinpolicy2 = spins_to_loop<125>::policy, template< class > class spinpolicy3 = spins_to_yield<250>::policy, template< class > class spinpolicy4 = spins_to_sleep::policy>
class quickcpplib::_xxx::configurable_spinlock::spinlock< T, spinpolicy2, spinpolicy3, spinpolicy4 >
A non-FIFO policy configurable spin lock meeting the Mutex concept providing the fastest possible spin lock.
- Template Parameters
-
| T | An integral type capable of atomic usage |
sizeof(spinlock<T>) == sizeof(T). Suitable for usage in shared memory.
Meets the requirements of BasicLockable and Lockable. Provides a get() and set() for the type used for the spin lock. Suitable for limited usage in constexpr.
- Warning
spinlock<bool> which might seem obvious is usually slower than spinlock<uintptr_t> on most architectures.
So why reinvent the wheel?
- Policy configurable spin.
- Implemented in pure C++ 11 atomics so the thread sanitiser works as expected.
- Multistate locks are possible instead of just 0|1.
- I don't much care for doing writes during the spin which a lot of other spinlocks do. It generates an unnecessary amount of cache line invalidation traffic. Better to spin-read and only write when the read suggests you might have a chance.
- This spin lock can use a pointer to memory as the spin lock at the cost of some performance. It uses the bottom bit as the locked flag. See locked_ptr<T>.