Public Member Functions | Protected Attributes | List of all members
ana::ThreadLocal< T > Class Template Reference

#include "/cvmfs/"

Public Member Functions

 ~ThreadLocal ()
Toperator-> ()

Protected Attributes

std::unordered_map< std::thread::id, T * > fVals
std::shared_mutex fMutex

Detailed Description

template<class T>
class ana::ThreadLocal< T >

A variable that has an independent value on each thread

Intended for use in implementing caches in a threadsafe manner. T must be default-constructible.

Definition at line 13 of file ThreadLocal.h.

Constructor & Destructor Documentation

template<class T>
ana::ThreadLocal< T >::~ThreadLocal ( )

Definition at line 16 of file ThreadLocal.h.

17  {
18  // No good way to know a thread exited, so we can't cleanup until here
19  for(auto it: fVals) delete it.second;
20  }
set< int >::iterator it
std::unordered_map< std::thread::id, T * > fVals
Definition: ThreadLocal.h:45

Member Function Documentation

template<class T>
T* ana::ThreadLocal< T >::operator-> ( )

Definition at line 22 of file ThreadLocal.h.

23  {
24  const std::thread::id id = std::this_thread::get_id();
26  {
27  // Normally it's safe for there to be multiple concurrent readers
28  std::shared_lock lock(fMutex);
30  auto it = fVals.find(id);
31  // We found the variable for this thread
32  if(it != fVals.end()) return it->second;
33  }
35  // If we've never seen this thread before we need to create a new entry,
36  // and we have to have exclusive access for that to be safe.
37  std::unique_lock lock(fMutex);
39  T* val = new T;
40  fVals.emplace(id, val);
41  return val;
42  }
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
std::shared_mutex fMutex
Definition: ThreadLocal.h:46
set< int >::iterator it
std::unordered_map< std::thread::id, T * > fVals
Definition: ThreadLocal.h:45
double T
Definition: Xdiff_gwt.C:5

Member Data Documentation

template<class T>
std::shared_mutex ana::ThreadLocal< T >::fMutex
template<class T>
std::unordered_map<std::thread::id, T*> ana::ThreadLocal< T >::fVals

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