![]() |
Home | Libraries | People | FAQ | More |
Boost.Fiber enables synchronizing fibers
running on different threads per default. This is acomblished on behalf of
spinlocks (using atomics inside). (BOOST_FIBERS_SPINLOCK_STD_MUTEX applied
at compiler's command line enables std::mutex
instead of spinlocks).
If synchronization between fibers (in different threads) is not desired,
BOOST_FIBERS_NO_ATOMICS
has to applied at compiler's command line.
A lock is considered under high contention, if a lock is free, but a thread subsequently fails to acquire the lock because some other thread was faster. Waiting for a short time let other threads finish before trying to enter the critical section again. While busy waiting on the lock, relaxing the CPU (via pause/yield memnonic) gives the CPU a hint that the code is in a spin-wait loop.
- prevents expensive pipeline flushes (speculatively executed load and compare instructions not pushed to pipeline) - hardware thread (simultaneous multithreading) gets time slice - delay of few CPU cycles
It is obvious that this strategy is useless on single core systems because
the lock can only released if the thread gives up its time slice in order
to let other threads run. Macro BOOST_FIBERS_SPIN_SINGLE_CORE disables active
spinning, e.g. der operating system is notified (via std::this_thread_yield()
) that the thread gives up its time slice
and the operating system switches to another thread. adaptive spin-wait loop
Macro BOOST_FIBERS_SPIN_MAX_TESTS determines how many times the CPU gets the spin-wait loop hint before yielding the thread or blocking in futex-wait. The spinlock tracks how many time the thread failed to acquire the lock. The higher the contention, the longer the thread should back-off. 'Binary Exponential Backoff' algorithm together with a randomized contention window is utilized for this purpose. BOOST_FIBERS_SPIN_MAX_COLLISIONS determines the upper limit of collisions between threads after the thread waits on a futex
Table 1.3. macros for tweaking
BOOST_FIBERS_NO_ATOMICS |
no multithreading, all atomics removed, no synchronization of fibers running in different threads |
---|---|
BOOST_FIBERS_SPINLOCK_STD_MUTEX |
|
BOOST_FIBERS_SPINLOCK_TTAS |
spinlock with test-test-and-swap on shared variable |
BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE |
spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries |
BOOST_FIBERS_SPINLOCK_TTAS_FUTEX |
spinlock with test-test-and-swap on shared variable, suspend on futex after certain amount of retries |
BOOST_FIBERS_SPINLOCK_TTAS_ADAPTIVE_FUTEX |
spinlock with test-test-and-swap on shared variable, while busy waiting adaptive retries, suspend on futex certain amount of retries |
BOOST_FIBERS_SPIN_SINGLE_CORE |
on single core machines with multiple threads, yield thread ( |
BOOST_FIBERS_SPIN_MAX_TESTS |
max amount of retries while busy spinning |
BOOST_FIBERS_SPIN_MAX_COLLISIONS |
max amount of collisions between contented threads |