ATLAS Offline Software
ThreadLocalHolder.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef G4ATLASTOOLS_THREADLOCALHOLDER_H
6 #define G4ATLASTOOLS_THREADLOCALHOLDER_H
7 
22 
23 
24 // System includes
25 #include <thread>
26 #include <utility>
27 
28 // Other includes
29 #include "tbb/concurrent_unordered_map.h"
30 
31 
32 namespace thread_utils
33 {
34 
47  template<class T>
49  {
50 
51  public:
52 
54  using MapVal_t = T*;
55  using MapHash_t = std::hash<MapKey_t>;
56  using ThreadMap_t =
57  tbb::concurrent_unordered_map< MapKey_t, MapVal_t, MapHash_t >;
58 
60  T* get() {
61  auto itr = m_threadMap.find( std::this_thread::get_id() );
62  if(itr == m_threadMap.end()) return nullptr;
63  return itr->second;
64  }
65 
67  void set(T* obj) {
68  const auto tid = std::this_thread::get_id();
69  m_threadMap.insert( std::make_pair(tid, obj) );
70  }
71 
73  const ThreadMap_t& getMap() const {
74  return m_threadMap;
75  }
76 
77  protected:
78 
81 
82  }; // class ThreadLocalHolder
83 
84 
92  template<class T>
94  {
95  public:
98  for(auto& mapPair : this->m_threadMap)
99  delete mapPair.second;
100  }
101  }; // class ThreadLocalOwner
102 
103 } // namespace thread_utils
104 
105 #endif
thread_utils::ThreadLocalOwner
A ThreadLocalHolder which owns its objects.
Definition: ThreadLocalHolder.h:94
thread_utils::ThreadLocalHolder< G4MagneticField >::MapHash_t
std::hash< MapKey_t > MapHash_t
Definition: ThreadLocalHolder.h:55
thread_utils::ThreadLocalHolder::getMap
const ThreadMap_t & getMap() const
Constant access for iteration, etc.
Definition: ThreadLocalHolder.h:73
thread_utils::ThreadLocalHolder::set
void set(T *obj)
Assign the object of the current thread.
Definition: ThreadLocalHolder.h:67
thread_utils::ThreadLocalHolder< G4MagneticField >::MapKey_t
std::thread::id MapKey_t
Definition: ThreadLocalHolder.h:53
thread_utils::ThreadLocalHolder::get
T * get()
Get the object of the current thread.
Definition: ThreadLocalHolder.h:60
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
thread_utils::ThreadLocalHolder< G4MagneticField >::MapVal_t
G4MagneticField * MapVal_t
Definition: ThreadLocalHolder.h:54
thread_utils::ThreadLocalHolder< G4MagneticField >::ThreadMap_t
tbb::concurrent_unordered_map< MapKey_t, MapVal_t, MapHash_t > ThreadMap_t
Definition: ThreadLocalHolder.h:57
thread_utils::ThreadLocalHolder::m_threadMap
ThreadMap_t m_threadMap
The wrapped thread-local storage container.
Definition: ThreadLocalHolder.h:80
thread_utils
Definition: ThreadLocalHolder.h:33
python.PyAthena.obj
obj
Definition: PyAthena.py:132
thread_utils::ThreadLocalOwner::~ThreadLocalOwner
~ThreadLocalOwner()
Destructor will clean up the storage.
Definition: ThreadLocalHolder.h:97
thread_utils::ThreadLocalHolder
A thread-local storage wrapper.
Definition: ThreadLocalHolder.h:49