folly::ThreadCachedInt

A thread-safe counter that caches increments in thread-local storage.

Synopsis

Declared in <folly/ThreadCachedInt.h>

template<
    class IntT,
    class Tag = IntT>
class ThreadCachedInt;

Member Functions

NameDescription
ThreadCachedInt [constructor]Constructors
operator= [deleted]Deleted copy assignment.
getCacheSize Returns the current per-thread cache size.
increment Adds a value to the counter, using the thread-local cache.
operator++ Pre-increments the counter (post-increment is not supported).
operator+= Adds a value to the counter.
operator-- Pre-decrements the counter (post-decrement is not supported).
operator-= Subtracts a value from the counter.
readFast Quickly grabs the current value which may not include some cached increments.
readFastAndReset Quickly reads and resets current value (doesn't reset cached increments).
readFull Reads the current value plus all the cached increments. Requires grabbing a lock, so this is significantly slower than readFast().
readFullAndReset This function is designed for accumulating into another counter, where you only want to count each increment once. It can still get the count a little off, however, but it should be much better than calling readFull() and set(0) sequentially.
set Thread-safe set function. This is a best effort implementation. In some edge cases, there could be data loss (missing counts)
setCacheSize Sets the per-thread cache size.