SPH
Classes | Public Member Functions | Friends | List of all members
ThreadLocal< Type > Class Template Reference

Template for storing a copy of a value for every thread in given scheduler. More...

#include <ThreadLocal.h>

Classes

class  LocalIterator
 

Public Member Functions

template<typename... TArgs>
 ThreadLocal (IScheduler &scheduler, TArgs &&... args)
 Constructs a thread-local storage from a list of values. More...
 
template<typename TFunctor >
 ThreadLocal (IScheduler &scheduler, TFunctor &&functor)
 Constructs a thread-local storage using a functor. More...
 
INLINE Type & local ()
 Return a value for current thread. More...
 
INLINE const Type & local () const
 Return a value for current thread. More...
 
INLINE Type & value (const Size threadId)
 Returns the storage corresponding to the thread with given index. More...
 
Type accumulate (const Type &initial=Type(0._f)) const
 Performs an accumulation of thread-local values. More...
 
template<typename TPredicate >
Type accumulate (const Type &initial, const TPredicate &predicate) const
 Performs an accumulation of thread-local values. More...
 
LocalIterator< Localbegin ()
 Returns the iterator to the first element in the thread-local storage. More...
 
LocalIterator< const Localbegin () const
 Returns the iterator to the first element in the thread-local storage. More...
 
LocalIterator< Localend ()
 Returns the iterator to the first element in the thread-local storage. More...
 
LocalIterator< const Localend () const
 Returns the iterator to the first element in the thread-local storage. More...
 

Friends

template<typename >
class ThreadLocal
 

Detailed Description

template<typename Type>
class ThreadLocal< Type >

Template for storing a copy of a value for every thread in given scheduler.

While C++ provides thread_local keyword for creating thread-local storages with static duration, ThreadLocal template can be used for local variables or (non-static) member variables of classes.

Definition at line 36 of file ThreadLocal.h.

Constructor & Destructor Documentation

◆ ThreadLocal() [1/2]

template<typename Type >
template<typename... TArgs>
ThreadLocal< Type >::ThreadLocal ( IScheduler scheduler,
TArgs &&...  args 
)
inline

Constructs a thread-local storage from a list of values.

Parameters
schedulerScheduler associated with the object.
argsList of parameters that are passed into the constructor of each thread-local storage.

Definition at line 70 of file ThreadLocal.h.

◆ ThreadLocal() [2/2]

template<typename Type >
template<typename TFunctor >
ThreadLocal< Type >::ThreadLocal ( IScheduler scheduler,
TFunctor &&  functor 
)
inline

Constructs a thread-local storage using a functor.

Parameters
schedulerScheduler associated with the object.
functorFunctor used to initialize each thread-local object.

Definition at line 80 of file ThreadLocal.h.

Member Function Documentation

◆ accumulate() [1/2]

template<typename Type >
template<typename TPredicate >
Type ThreadLocal< Type >::accumulate ( const Type &  initial,
const TPredicate &  predicate 
) const
inline

Performs an accumulation of thread-local values.

Uses provided binary predicate to accumulate the values.

Parameters
initialValue to which the accumulated result is initialized.
predicateCallable object with signature Type operator()(const Type&, const Type&).

Definition at line 123 of file ThreadLocal.h.

◆ accumulate() [2/2]

template<typename Type >
Type ThreadLocal< Type >::accumulate ( const Type &  initial = Type(0._f)) const
inline

Performs an accumulation of thread-local values.

Uses operator + to sum up the elements.

Parameters
initialValue to which the accumulated result is initialized.

Definition at line 113 of file ThreadLocal.h.

◆ begin() [1/2]

template<typename Type >
LocalIterator<Local> ThreadLocal< Type >::begin ( )
inline

Returns the iterator to the first element in the thread-local storage.

Definition at line 145 of file ThreadLocal.h.

◆ begin() [2/2]

template<typename Type >
LocalIterator<const Local> ThreadLocal< Type >::begin ( ) const
inline

Returns the iterator to the first element in the thread-local storage.

Definition at line 150 of file ThreadLocal.h.

◆ end() [1/2]

template<typename Type >
LocalIterator<Local> ThreadLocal< Type >::end ( )
inline

Returns the iterator to the first element in the thread-local storage.

Definition at line 155 of file ThreadLocal.h.

◆ end() [2/2]

template<typename Type >
LocalIterator<const Local> ThreadLocal< Type >::end ( ) const
inline

Returns the iterator to the first element in the thread-local storage.

Definition at line 160 of file ThreadLocal.h.

◆ local() [1/2]

template<typename Type >
INLINE Type& ThreadLocal< Type >::local ( )
inline

Return a value for current thread.

This thread must belong the the thread pool given in constructor, checked by assert.

Definition at line 88 of file ThreadLocal.h.

◆ local() [2/2]

template<typename Type >
INLINE const Type& ThreadLocal< Type >::local ( ) const
inline

Return a value for current thread.

This thread must belong the the thread pool given in constructor, checked by assert.

Definition at line 95 of file ThreadLocal.h.

◆ value()

template<typename Type >
INLINE Type& ThreadLocal< Type >::value ( const Size  threadId)
inline

Returns the storage corresponding to the thread with given index.

Can be called from any thread. There is no synchronization, so accessing the storage from the associated worker at the same time might cause a race condition.

Definition at line 105 of file ThreadLocal.h.

Friends And Related Function Documentation

◆ ThreadLocal

template<typename Type >
template<typename >
friend class ThreadLocal
friend

Definition at line 39 of file ThreadLocal.h.


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