Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Public Member Functions | Private Attributes | List of all members
Threads::ThreadLocalStorage< T > Class Template Reference

A class that provides a separate storage location on each thread that accesses the object. More...

#include <thread_local_storage.h>

Inheritance diagram for Threads::ThreadLocalStorage< T >:
[legend]

Public Member Functions

 ThreadLocalStorage ()
 
 ThreadLocalStorage (const T &t)
 
 ThreadLocalStorage (const ThreadLocalStorage< T > &t)
 
T & get ()
 
T & get (bool &exists)
 
 operator T & ()
 
ThreadLocalStorage< T > & operator= (const T &t)
 
tbb::enumerable_thread_specific
< T > & 
get_implementation ()
 

Private Attributes

tbb::enumerable_thread_specific
< T > 
data
 

Detailed Description

template<typename T>
class Threads::ThreadLocalStorage< T >

A class that provides a separate storage location on each thread that accesses the object.

This class offers ways so that every thread that accesses it has its own copy of an object of type T. In essence, accessing this object can never result in race conditions in multithreaded programs since no other thread than the current one can ever access it.

The class builds on the Threading Building Blocks's tbb::enumerable_thread_specific class but wraps it in such a way that this class can also be used when deal.II is configured not to use threads at all – in that case, this class simply stores a single copy of an object of type T.

Construction and destruction

Objects of this class can either be default constructed or by providing an "exemplar", i.e. an object of type T so that every time we need to create a T on a thread that doesn't already have such an object, it is copied from the exemplar.

Upon destruction of objects of this class, all T objects that correspond to threads that have accessed this object are destroyed. Note that this may be before the time when a thread is terminated.

Access

The T object stored by this object can be accessed using the get() function. It provides a reference to a unique object when accessed from different threads. Objects of type T are created lazily, i.e. they are only created whenever a thread actually calls get().

Definition at line 69 of file thread_local_storage.h.

Constructor & Destructor Documentation

template<typename T >
Threads::ThreadLocalStorage< T >::ThreadLocalStorage ( )
inline

Default constructor. Initialize each thread local object using its default constructor.

Definition at line 165 of file thread_local_storage.h.

template<typename T>
Threads::ThreadLocalStorage< T >::ThreadLocalStorage ( const T &  t)
inlineexplicit

A kind of copy constructor. Initialize each thread local object by copying the given object.

Definition at line 171 of file thread_local_storage.h.

template<typename T>
Threads::ThreadLocalStorage< T >::ThreadLocalStorage ( const ThreadLocalStorage< T > &  t)
inline

Copy constructor. Initialize each thread local object with the corresponding object of the given object.

Definition at line 179 of file thread_local_storage.h.

Member Function Documentation

template<typename T >
T & Threads::ThreadLocalStorage< T >::get ( )
inline

Return a reference to the data stored by this object for the current thread this function is called on.

Note that there is no member function get() that is const and returns a const reference as one would expect. The reason is that if such a member function were called on a thread for which no thread-local object has been created yet, then one has to create such an object first which would certainly be a non-constant operation. If you need to call the get() function for a member variable of a class from a const member function, then you need to declare the member variable mutable to allow such access.

Definition at line 188 of file thread_local_storage.h.

template<typename T >
T & Threads::ThreadLocalStorage< T >::get ( bool exists)
inline

Same as above, except that exists is set to true if an element was already present for the current thread; false otherwise.

Definition at line 201 of file thread_local_storage.h.

template<typename T >
Threads::ThreadLocalStorage< T >::operator T & ( )
inline

Conversion operator that simply converts the thread-local object to the data type that it stores. This function is equivalent to calling the get() member function; it's purpose is to make the TLS object look more like the object it is storing.

Definition at line 214 of file thread_local_storage.h.

template<typename T>
ThreadLocalStorage< T > & Threads::ThreadLocalStorage< T >::operator= ( const T &  t)
inline

Copy the given argument into the storage space used to represent the current thread. Calling this function as tls_data = object is equivalent to calling tls_data.get() = object. The intent of this operator is to make the ThreadLocalStorage object look more like the object it represents on the current thread.

Parameters
tThe object to be copied into the storage space used for the current thread.
Returns
The current object, after the changes have been made

Definition at line 223 of file thread_local_storage.h.

template<typename T>
tbb::enumerable_thread_specific<T>& Threads::ThreadLocalStorage< T >::get_implementation ( )
inline

Returns a reference to the internal implementation.

Definition at line 142 of file thread_local_storage.h.

Member Data Documentation

template<typename T>
tbb::enumerable_thread_specific<T> Threads::ThreadLocalStorage< T >::data
private

The data element we store. If we support threads, then this object will be of a type that provides a separate object for each thread. Otherwise, it is simply a single object of type T.

Definition at line 155 of file thread_local_storage.h.


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