carb::thread::AtomicBackoff
Defined in carb/thread/Util.h
-
template<size_t PausesBeforeYield = 16>
class AtomicBackoff A utility class for providing a growing number of pause instructions, followed by yielding.
The pause instruction is effectively CARB_HARDWARE_PAUSE().
Very fast to construct, often used in a loop such as:
for (AtomicBackoff<> b;; b.pause()) if (condition()) break;
- Template Parameters
PausesBeforeYield – the number of pauses that will occur before yielding begins. See AtomicBackoff::pause().
Public Functions
-
constexpr AtomicBackoff() noexcept = default
Constructor.
-
inline void reset() noexcept
Resets
*this
.
-
inline void pause() noexcept
Every time called, will pause for exponentially longer, and after a certain amount will instead yield.
Pause is as via CARB_HARDWARE_PAUSE. Yield is as via
std::this_thread::yield()
. The pause count starts at 1 when reset() or newly constructed. Each time this function is called, the CPU pause instruction is executedcount
times andcount
is doubled. If called whencount
exceeds kPausesBeforeYield, a yield occurs instead. Calling reset() resets thecount
to 1.
-
inline bool pauseWithoutYield() noexcept
Similar to pause() but if would yield, instead returns false.
- Returns
true if the internal
count
is less than kPausesBeforeYield;false
otherwise.
Public Static Functions
-
static inline void pauseLoop(size_t count) noexcept
A helper function for executing the CPU pause instruction
count
times.- Parameters
count – The number of times to execute CARB_HARDWARE_PAUSE().
Public Static Attributes
-
static constexpr size_t kPausesBeforeYield = PausesBeforeYield
The number of pauses that should be executed before yielding the thread.