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 (return true) before continuing, but f will only succeed for one thread at a time. This function does not return until f returns true, at which point this function returns immediately. High contention is assumed when f returns false for several calls, at which point the calling thread will invoke std::this_thread::yield() between calls to f. 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 returned true 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.