Click
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
per_thread< T > Class Template Reference

#include <sync.hh>

Inherited by per_thread_oread< State >, and per_thread_oread< T >.

Public Member Functions

 per_thread (T v)
 
 per_thread (T v, int n)
 
void resize (unsigned int max_cpu_id, T v)
 
T * operator-> () const
 
T & operator* () const
 
T & operator+= (const T &add) const
 
T & operator++ () const
 
T operator++ (int) const
 
T & operator-- () const
 
T operator-- (int) const
 
T & operator= (T value) const
 
void operator= (const per_thread< T > &pt)
 
void set (T v)
 
void setAll (T v)
 
T & get () const
 
const T & cst () const
 
T & get_value_for_thread (int thread_id) const
 
void set_value_for_thread (int thread_id, T v)
 
T & get_value (int i) const
 
void set_value (int i, T v)
 
unsigned weight () const
 
unsigned int get_mapping (int i)
 
const_iterator begin () const
 
const_iterator end () const
 
iterator begin ()
 
iterator end ()
 

Protected Attributes

AT * storage
 
unsigned _size
 

Detailed Description

template<typename T>
class per_thread< T >

Duplicate a variable per-thread, allowing all threads to have their own version of a variable. As opposed to __thread, any thread can also access other thread's values using get/set_value_for_thread(), allowing proper initialization and cleaning from the only thread calling elements initialize and cleanup functions using a for loop from 0 to size().

The class provides convenient functions, eg : per_thread<int> my_int;

my_int++; my_int = my_int + 1; int &a = *my_int; //Return a reference a++; //Affect the per-thread variable

per_thread<struct foo> my_foo; my_foo->bar++;

IMPORTANT: The variable will be cached-align to avoid false sharing. This means that the amount of bytes for each thread will be rounded up to 64 bytes. That means that you should make per_thread of big structure of variables instead of multiple per_threads.

In other term, prefer to do : struct state { int count; int drop; ... } per_thread<struct state> _state;

Instead of : //this is the bad version per_thread<int> _count; //do not copy-cut per_thread<int> _drop; //do not do this ...

Member Function Documentation

◆ resize()

template<typename T >
void per_thread< T >::resize ( unsigned int  max_cpu_id,
T  v 
)
inline

Resize must be called if per_thread was initialized before click_max_cpu_ids() is set (such as in static functions) This will destroy all data

◆ get_value_for_thread()

template<typename T >
T & per_thread< T >::get_value_for_thread ( int  thread_id) const
inline

get_value_for_thread get the value for a given thread id Do not do for (int i = 0; i < size(); i++) get_value_for_thread as in omem and oread version, not all threads are represented, this would lead to a bug !

◆ get_value()

template<typename T >
T & per_thread< T >::get_value ( int  i) const
inline

get_value can be used to iterate around all per-thread variables. On the normal version it is the same as get_value_for_thread, but on omem and oread version of per_thread, this iterate only over thread that will really be used.

◆ weight()

template<typename T >
unsigned per_thread< T >::weight ( ) const
inline

Number of elements inside the vector. This may not the number of threads, only use this to iterate with get_value()/set_value(), not get_value_for_thread()


The documentation for this class was generated from the following file: