FOLLY: ThreadCachedInt.h (1)














































FOLLY: ThreadCachedInt.h (1)



High performance atomic increment using thread caching.
folly/ThreadCachedInt.h introduces an integer class designed for high-performance increments from multiple threads simultaneously without loss of precision. It has two read modes, readFast gives a potentially stale value in one read and readFull gives the exact value but is much slower as shown below.
Performance
________________________________________
The performance increase is up to 10x greater than std::atomic_fetch_add in high contention environments. More detailed benchmarks can be found at folly/test/ThreadCachedIntTest.h.
readFast is as fast as a single read.
readFull, on the other hand, requires acquiring a mutex and iterating through the list to collect the values ​​of all thread-local counters, so it is significantly slower than readFast.

Usage


Create an instance and increment it with increment or the operator overloads. Read the value with readFast for quick, potentially stale data, or readFull for a more expensive but precise result. There are additional convenience functions as well, such as set.

    ThreadCachedInt<int64_t> val;

    EXPECT_EQ(0, val.readFast());

    ++val;                        // increment in thread local counter only

    EXPECT_EQ(0, val.readFast()); // increment has not been flushed

    EXPECT_EQ(1, val.readFull()); // accumulates all thread local counters

    val.set(2);

    EXPECT_EQ(2, val.readFast());

    EXPECT_EQ(2, val.readFull());




Comments