carb::this_thread::spinWaitWithBackoff
Defined in carb/thread/Util.h
-
template<class Func>
void carb::this_thread::spinWaitWithBackoff(Func &&f) noexcept(noexcept(f())) Calls a predicate until it returns true with progressively increasing delays between calls.
This function is a low-level utility for high-contention cases where multiple threads will be calling
f
simultaneously,f
needs to succeed (returntrue
) before continuing, butf
will only succeed for one thread at a time. This function does not return untilf
returnstrue
, at which point this function returns immediately. High contention is assumed whenf
returnsfalse
for several calls, at which point the calling thread will invokestd::this_thread::yield()
between calls tof
. This is a back-off mechanism to allow one thread to move forward while other competing threads wait their turn.Note
This function does not return until
f
has returnedtrue
for the calling thread. For a version of this function that will give up, use spinTryWaitWithBackoff().Note
This function may enter the kernel to wait.
- Parameters
f – The predicate to call repeatedly until it returns
true
.