The SKIRT project
advanced radiative transfer for astrophysics
Classes | Public Member Functions | Private Types | Static Private Attributes | List of all members
ThreadLocalMember< T > Class Template Reference

#include <ThreadLocalMember.hpp>

Classes

struct  Repository
 

Public Member Functions

vector< T * > all ()
 
T * local ()
 

Private Types

using RepositoryRegistry = std::unordered_set< Repository * >
 

Static Private Attributes

static RepositoryRegistry allRepositories
 
static std::mutex mutex
 
static thread_local Repository repository
 

Detailed Description

template<class T>
class ThreadLocalMember< T >

An instance of the ThreadLocalMember<T> class template provides a thread-local data member of type T. In other words, there is a seperate copy of the data member for each instance of the embedding object, and for each thread referring to the data member. A reference to the thread-local copy of the data member can be obtained at the cost of a single std::unordered_map look-up, which has constant-time performance on average.

The type T must be default-constructable, because each copy is constructed automatically in the background when a reference to it is requested for the first time. A copy is destructed when the thread for which it was created (the associated thread) is destructed.

The class also offers a function to obtain a list of pointers to the copies of the data member for all threads that ever referred to it (and that are not yet destructed). For example, when a data member is employed as a thread-local buffer, this capability allows a client to "flush" the contents of the buffer after multi-threaded operation has ceased (but before the threads are actually destroyed).

The mechanisms and functions of this class are fully thread-safe. However, the all() function breaks thread-locality; any access to data member copies associated with other threads should be properly synchronized. Also, the pointers returned by the all() function become invalid when the threads associated with the referenced data members are destructed.

Member Function Documentation

◆ all()

template<class T >
vector< T * > ThreadLocalMember< T >::all ( )
inline

This function returns a list of pointers to the copies of the data member for all threads that ever referred to it and that are not yet destructed. The list is in arbitrary order. Access to data member copies associated with other threads should be properly synchronized. Also, the pointers returned by this function become invalid when the threads associated with the referenced data members are destructed

◆ local()

template<class T >
T * ThreadLocalMember< T >::local ( )
inline

This function returns a pointer to the thread-local copy of the data member.


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