carb::this_thread::spinTryWait
Defined in carb/thread/Util.h
-
template<class Func>
bool carb::this_thread::spinTryWait(Func &&f) noexcept(noexcept(f())) Calls a predicate until it returns true or a random number of attempts have elapsed.
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 picks a pseudo-random maximum number of times to call the function (the randomness is so that multiple threads will not choose the same number and perpetually block each other) and repeatedly calls the function that number of times. Iff
returnstrue
, spinTryWait() immediately returnstrue
.- Parameters
f – The predicate to call repeatedly until it returns
true
.- Returns
true
immediately whenf
returnstrue
. If a random number of attempts to callf
all returnfalse
thenfalse
is returned.