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, and f needs to succeed (return true) before continuing, but f will only succeed for one thread at a time. This function will call f interspersed with increasing invocations of CARB_HARDWARE_PAUSE. However, unlike spinTryWait(), this function will then proceed to std::this_thread::yield() calls which will typically enter the kernel to wait. After a certain number of attempts, the function gives up and returns false. If f returns true, spinTryWait() immediately returns true.

Note

This function may enter the kernel to wait.

Parameters

f – The predicate to call repeatedly until it returns true.

Returns

true immediately when f returns true. If a number of attempts to call f all return false then the function gives up and false is returned.