GteThreadSafeMap.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <GTEngineDEF.h>
11 #include <map>
12 #include <mutex>
13 #include <vector>
14 
15 namespace gte
16 {
17 
18 template <typename Key, typename Value>
20 {
21 public:
22  // Construction and destruction.
23  virtual ~ThreadSafeMap();
24  ThreadSafeMap();
25 
26  // All the operations are thread-safe.
27  bool HasElements() const;
28  bool Exists(Key key) const;
29  void Insert(Key key, Value value);
30  bool Remove(Key key, Value& value);
31  void RemoveAll();
32  bool Get(Key key, Value& value) const;
33  void GatherAll(std::vector<Value>& values) const;
34 
35 protected:
36  std::map<Key,Value> mMap;
37  mutable std::mutex mMutex;
38 };
39 
40 
41 template <typename Key, typename Value>
43 {
44 }
45 
46 template <typename Key, typename Value>
48 {
49 }
50 
51 template <typename Key, typename Value>
53 {
54  bool hasElements;
55  mMutex.lock();
56  {
57  hasElements = (mMap.size() > 0);
58  }
59  mMutex.unlock();
60  return hasElements;
61 }
62 
63 template <typename Key, typename Value>
65 {
66  bool exists;
67  mMutex.lock();
68  {
69  exists = (mMap.find(key) != mMap.end());
70  }
71  mMutex.unlock();
72  return exists;
73 }
74 
75 template <typename Key, typename Value>
77 {
78  mMutex.lock();
79  {
80  mMap[key] = value;
81  }
82  mMutex.unlock();
83 }
84 
85 template <typename Key, typename Value>
87 {
88  bool exists;
89  mMutex.lock();
90  {
91  auto iter = mMap.find(key);
92  if (iter != mMap.end())
93  {
94  value = iter->second;
95  mMap.erase(iter);
96  exists = true;
97  }
98  else
99  {
100  exists = false;
101  }
102  }
103  mMutex.unlock();
104  return exists;
105 }
106 
107 template <typename Key, typename Value>
109 {
110  mMutex.lock();
111  {
112  mMap.clear();
113  }
114  mMutex.unlock();
115 }
116 
117 template <typename Key, typename Value>
118 bool ThreadSafeMap<Key, Value>::Get(Key key, Value& value) const
119 {
120  bool exists;
121  mMutex.lock();
122  {
123  auto iter = mMap.find(key);
124  if (iter != mMap.end())
125  {
126  value = iter->second;
127  exists = true;
128  }
129  else
130  {
131  exists = false;
132  }
133  }
134  mMutex.unlock();
135  return exists;
136 }
137 
138 template <typename Key, typename Value>
139 void ThreadSafeMap<Key, Value>::GatherAll(std::vector<Value>& values) const
140 {
141  mMutex.lock();
142  {
143  if (mMap.size() > 0)
144  {
145  values.resize(mMap.size());
146  auto viter = values.begin();
147  for (auto const& m : mMap)
148  {
149  *viter++ = m.second;
150  }
151  }
152  else
153  {
154  values.clear();
155  }
156  }
157  mMutex.unlock();
158 }
159 
160 
161 }
void Insert(Key key, Value value)
std::map< Key, Value > mMap
const GLfloat * m
Definition: glext.h:6461
bool Get(Key key, Value &value) const
GLsizei const GLfloat * value
Definition: glcorearb.h:819
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1597
bool Remove(Key key, Value &value)
bool Exists(Key key) const
void GatherAll(std::vector< Value > &values) const
bool HasElements() const


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01