Back to Taskflow

template<typename T> tf::CachelineAligned class

docs/classtf_1_1CachelineAligned.html

4.0.01.5 KB
Original Source

template<typename T> tf::CachelineAligned class

class to ensure cacheline-aligned storage for an object.

Template parameters
T

This utility class aligns the stored object data to twice the size of a cacheline. The alignment improves performance by optimizing data access in cache-sensitive scenarios.

// create two integers on two separate cachelines to avoid false sharingtf::CachelineAligned\<int\> counter1;tf::CachelineAligned\<int\> counter2;// two threads access the two counters without false sharingstd::thread t1([&]{ counter1.get() = 1; });std::thread t2([&]{ counter2.get() = 2; });t1.join();t2.join();

Public functions

auto get() -> T&accesses the underlying objectauto get() const -> const T&accesses the underlying object as a constant reference

Public variables

T dataThe stored object, aligned to twice the cacheline size.

Function documentation

template<typename T> T& tf::CachelineAligned<T>::get()

accesses the underlying object

| Returns | a reference to the underlying object. |

template<typename T> const T& tf::CachelineAligned<T>::get() const

accesses the underlying object as a constant reference

| Returns | a constant reference to the underlying object. |