Class spin_mutex¶
Defined in File spin_mutex.hpp
Class Documentation¶
-
class
concore::v1::spin_mutex¶ Mutex class that uses CPU spinning while attempting to take the lock.
For mutexes that protect very small regions of code, a spin_mutex can be much faster than a traditional mutex. Instead of taking a lock, this will spin on the CPU, trying to avoid yielding the CPU quanta.
This uses an exponential backoff spinner. If after some time doing small waits it cannot enter the critical section, it will yield the CPU quanta of the current thread.
Spin mutexes should only be used to protect very-small regions of code; a handful of CPU instructions. For larger scopes, a traditional mutex may be faster; but then, think about using serializer to avoid mutexes completely.
- See
Public Functions
-
spin_mutex() = default¶ Default constructor.
Constructs a spin mutex that is not acquired by any thread.
-
~spin_mutex() = default¶ Destructor.
-
spin_mutex(const spin_mutex&) = delete¶ Copy constructor is DISABLED.
-
spin_mutex &
operator=(const spin_mutex&) = delete¶ Copy assignment is DISABLED.
-
spin_mutex(spin_mutex&&) = delete¶ Move constructor is DISABLED.
-
spin_mutex &
operator=(spin_mutex&&) = delete¶ Move assignment is DIABLED.
-
void
lock()¶ Acquires ownership of the mutex.
Uses a spin_backoff to spin while waiting for the ownership to be free. When exiting this function the mutex will be owned by the current thread.
An unlock() call must be made for each call to lock().
- See
-
bool
try_lock()¶ Tries to lock the mutex; returns false if the mutex is not available.
This is similar to
lock() but does not wait for the mutex to be free again. If the mutex is acquired by a different thread, this will return false.- Return
True if the mutex ownership was acquired; false if the mutex is busy
An unlock() call must be made for each call to this method that returns true.
-
void
unlock()¶ Releases the ownership on the mutex.
This needs to be called for every lock() and for every try_lock() that returns true. It should not be called without a matching lock() or try_lock().
- See