mutexpp
is a collection of C++ mutex implementations and utilities.
A mutex that, upon locking, will halt a thread's execution until that mutex is acquired.
These types of mutexes are best used when the critical sections they protect are relatively long (such as I/O or OS API calls).
When blocked, the thread consumes almost no computer resources.
It is very slow to transition a blocking mutex from unlocked to locked.
A mutex that, upon locking, will loop a thread's execution until that mutex is acquired.
These types of mutexes are best used when the critical sections they protect are relatively brief (such as memory-resident data modifications).
When blocked, the thread will spike a CPU.
It is very fast to transition a spin mutex from unlocked to locked.
A mutex that, upon locking, will first spin, then halt a thread's execution until that mutex is acquired. The amount of time taken during the initial spin is adjusted such that the lock acquistion happens more often during the spin phase.
These mutexes are best used when spin mutexes would suffice, but occasionally a critical section may take an unusually long amount of time, and it would be better to block the thread.
Tries to maximize throughput while minimizing computer resource consumption. The spin phase is an attempt to avoid the expensive cost of blocking the thread, while the blocking phase attempts to avoid the expense of spiking a CPU in the event locking is taking too long.
If the time required to lock the mutex is consistently large (i.e., beyond the time it takes to block/unblock a thread) you'll be spending time spinning when you're better off using a blocking mutex.
Process scheduling is not considered.
After every mutex lock, we adjust the expected spin time. Given
- A description of
pthread
's adaptive mutex from the original implementer, Kaz Kylheku - Intel TBB description of mutex flavors.
- POSIX API
clock_gettime
- Wall v. CPU clock example implementation
- Example clock calls on various OSes.
mach_absolute_time