carb::this_thread::spinTryWaitWithBackoff
Defined in carb/thread/Util.h
-
template<class Func>
bool carb::this_thread::spinTryWaitWithBackoff(Func &&f) noexcept(noexcept(f())) Calls a predicate until it returns true or a number of attempts have elapsed, backing off as time passes.
This function is a low-level utility for high-contention cases where multiple threads will be calling
f
simultaneously, andf
needs to succeed (returntrue
) before continuing, butf
will only succeed for one thread at a time. This function will callf
interspersed with increasing invocations of CARB_HARDWARE_PAUSE. However, unlike spinTryWait(), this function will then proceed tostd::this_thread::yield()
calls which will typically enter the kernel to wait. After a certain number of attempts, the function gives up and returnsfalse
. Iff
returnstrue
, spinTryWait() immediately returnstrue
.Note
This function may enter the kernel to wait.
- Parameters
f – The predicate to call repeatedly until it returns
true
.- Returns
true
immediately whenf
returnstrue
. If a number of attempts to callf
all returnfalse
then the function gives up andfalse
is returned.