-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathConcurrentMap.h
More file actions
62 lines (51 loc) · 1.63 KB
/
ConcurrentMap.h
File metadata and controls
62 lines (51 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef ConcurrentMap_h
#define ConcurrentMap_h
#include <shared_mutex>
#include "robin_hood.h"
namespace tns {
template<class TKey, class TValue>
class ConcurrentMap {
public:
inline void Insert(TKey& key, TValue value) {
std::lock_guard<std::mutex> writerLock(this->containerMutex_);
this->container_[key] = value;
}
inline TValue Get(TKey& key) {
bool found;
return this->Get(key, found);
}
inline TValue Get(TKey& key, bool& found) {
std::lock_guard<std::mutex> writerLock(this->containerMutex_);
auto it = this->container_.find(key);
found = it != this->container_.end();
if (found) {
return it->second;
}
return nullptr;
}
inline bool ContainsKey(TKey& key) {
std::lock_guard<std::mutex> writerLock(this->containerMutex_);
auto it = this->container_.find(key);
return it != this->container_.end();
}
inline void Remove(TKey& key) {
std::lock_guard<std::mutex> writerLock(this->containerMutex_);
this->container_.erase(key);
}
inline void ForEach(const std::function<bool(TKey&, TValue&)>& func) {
std::lock_guard<std::mutex> writerLock(this->containerMutex_);
for(auto i : this->container_) {
if(func(i.first, i.second)) {
break;
}
}
}
ConcurrentMap() = default;
ConcurrentMap(const ConcurrentMap&) = delete;
ConcurrentMap& operator=(const ConcurrentMap&) = delete;
private:
std::mutex containerMutex_;
robin_hood::unordered_map<TKey, TValue> container_;
};
}
#endif /* ConcurrentMap_h */