ATLAS Offline Software
Loading...
Searching...
No Matches
ThreadActionHolder.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__G4UA_THREADACTIONHOLDER_H
6#define G4ATLASTOOLS__G4UA_THREADACTIONHOLDER_H
7
8// System includes
9#include <thread>
10#include <memory>
11#include <utility>
12
13// Other includes
14#include "tbb/concurrent_unordered_map.h"
15
16namespace G4UA
17{
18
29 template<class ActionType>
31 {
32
33 public:
34
35 using ThreadMapKey_t = std::thread::id;
36 using ThreadMapVal_t = ActionType*;
37 //using ThreadMapVal_t = std::unique_ptr<ActionType>; // not supported
38 using ThreadMapHash_t = std::hash<ThreadMapKey_t>;
39
40 using ThreadMap_t = tbb::concurrent_unordered_map
42
43 using const_iterator = typename ThreadMap_t::const_iterator;
44
48 for(auto mapPair : m_threadMap){
49 delete mapPair.second;
50 }
51 m_threadMap.clear();
52 }
53
55 ActionType* get() {
56 auto mapPair = m_threadMap.find( std::this_thread::get_id() );
57 if(mapPair == m_threadMap.end()) return nullptr;
58 return mapPair->second;
59 }
60
63 void set(std::unique_ptr<ActionType> action) {
64 const auto tid = std::this_thread::get_id();
65 m_threadMap.insert( std::make_pair(tid, action.release()) );
66 }
67
70 return m_threadMap.begin();
71 }
72
75 return m_threadMap.end();
76 }
77
78 private:
79
82
83 }; // class ThreadActionHolder
84
85}
86
87#endif
A thread-local storage wrapper for the user actions.
const_iterator end() const
Constant-access iteration over the action map.
tbb::concurrent_unordered_map< ThreadMapKey_t, ThreadMapVal_t, ThreadMapHash_t > ThreadMap_t
std::hash< ThreadMapKey_t > ThreadMapHash_t
void set(std::unique_ptr< ActionType > action)
Assign the object of the current thread.
typename ThreadMap_t::const_iterator const_iterator
const_iterator begin() const
Constant-access iteration over the action map.
ThreadMap_t m_threadMap
The wrapped thread-local storage container.
ActionType * get()
Get the object of the current thread.
~ThreadActionHolder()
Destructor will clean up the thread-local storage.