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, and f needs to succeed (return true) before continuing, but f 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. If f returns true, spinTryWait() immediately returns true.

Parameters

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

Returns

true immediately when f returns true. If a random number of attempts to call f all return false then false is returned.