ATLAS Offline Software
Loading...
Searching...
No Matches
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
32namespace thread_utils
33{
34
47 template<class T>
49 {
50
51 public:
52
53 using MapKey_t = std::thread::id;
54 using MapVal_t = T*;
55 using MapHash_t = std::hash<MapKey_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
A thread-local storage wrapper.
T * get()
Get the object of the current thread.
void set(T *obj)
Assign the object of the current thread.
ThreadMap_t m_threadMap
The wrapped thread-local storage container.
const ThreadMap_t & getMap() const
Constant access for iteration, etc.
tbb::concurrent_unordered_map< MapKey_t, MapVal_t, MapHash_t > ThreadMap_t
A ThreadLocalHolder which owns its objects.
~ThreadLocalOwner()
Destructor will clean up the storage.