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

#include "/cvmfs/nova.opensciencegrid.org/externals/cafanacore/v01.14/src/CAFAna/Core/ThreadLocal.h"

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 ( )
inline

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-> ( )
inline

Definition at line 22 of file ThreadLocal.h.

23  {
24  const std::thread::id id = std::this_thread::get_id();
25 
26  {
27  // Normally it's safe for there to be multiple concurrent readers
28  std::shared_lock lock(fMutex);
29 
30  auto it = fVals.find(id);
31  // We found the variable for this thread
32  if(it != fVals.end()) return it->second;
33  }
34 
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);
38 
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
protected
template<class T>
std::unordered_map<std::thread::id, T*> ana::ThreadLocal< T >::fVals
protected

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