DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
da_concurrent_vector.hpp
1#ifndef DA_CONCURRENT_VECTOR_HPP
2#define DA_CONCURRENT_VECTOR_HPP
3#include <vector>
4#include <mutex>
5
18template< typename T >
20{
21public:
22 using vector_type = std::vector< T >;
23 using value_type = vector_type::value_type;
24 using reference = vector_type::reference;
25 using const_reference = vector_type::const_reference;
26 using size_type = vector_type::size_type;
27 using iterator = vector_type::iterator;
28 using const_iterator = vector_type::const_iterator;
29 using mutex_type = std::mutex;
30 using lock_guard_type = std::lock_guard< mutex_type >;
31 using unique_lock_type = std::unique_lock< mutex_type >;
32
33public:
35 da_concurrent_vector(size_type count);
36 da_concurrent_vector(std::initializer_list< T > init);
38 reference front();
39 const_reference front() const;
40 reference back();
41 const_reference back() const;
42 bool empty() const;
43 size_type size() const;
44 void clear();
45 iterator insert(iterator pos, const T& value);
46 iterator insert(const_iterator pos, const T& value);
47 void insert(iterator pos, size_type count, const T& value);
48 iterator insert(const_iterator pos, size_type count, const T& value);
49 void push_back(const T& value);
50 void push_front(const T& value);
51 void pop_front();
52 void pop_back();
53 iterator erase(iterator pos);
54 iterator erase(const_iterator pos);
55 iterator erase(iterator first, iterator last);
56 iterator erase(const_iterator first, const_iterator last);
57 iterator begin();
58 const_iterator begin() const;
59 iterator end();
60 const_iterator end() const;
61
62private:
63 vector_type< T > m_inner_list;
64 mutable mutex_type m_mutex;
65};
66
67template< typename T >
69{
70}
71
72template< typename T >
73da_concurrent_vector< T >::da_concurrent_vector(da_concurrent_vector::size_type count) : m_inner_list(count)
74{
75}
76
77template< typename T >
78da_concurrent_vector< T >::da_concurrent_vector(std::initializer_list< T > init) : m_inner_list(init)
79{
80}
81
82template< typename T >
84{
85}
86
87template< typename T >
88da_concurrent_vector< T >::reference da_concurrent_vector< T >::front()
89{
90 unique_lock_type lg(m_mutex);
91
92 return (m_inner_list.front());
93}
94
95template< typename T >
96da_concurrent_vector< T >::const_reference da_concurrent_vector< T >::front() const
97{
98 unique_lock_type lg(m_mutex);
99
100 return (m_inner_list.front());
101}
102
103template< typename T >
104da_concurrent_vector< T >::reference da_concurrent_vector< T >::back()
105{
106 unique_lock_type lg(m_mutex);
107
108 return (m_inner_list.back());
109}
110
111template< typename T >
112da_concurrent_vector< T >::const_reference da_concurrent_vector< T >::back() const
113{
114 unique_lock_type lg(m_mutex);
115
116 return (m_inner_list.back());
117}
118
119template< typename T >
121{
122 unique_lock_type lg(m_mutex);
123
124 return (m_inner_list.empty());
125}
126
127template< typename T >
128da_concurrent_vector< T >::size_type da_concurrent_vector< T >::size() const
129{
130 unique_lock_type lg(m_mutex);
131
132 return (m_inner_list.size());
133}
134
135template< typename T >
137{
138 unique_lock_type lg(m_mutex);
139
140 m_inner_list.clear();
141}
142
143template< typename T >
144da_concurrent_vector< T >::iterator da_concurrent_vector< T >::insert(da_concurrent_vector< T >::iterator pos, const T& value)
145{
146 unique_lock_type lg(m_mutex);
147
148 return (m_inner_list.insert(pos, value));
149}
150
151template< typename T >
152da_concurrent_vector< T >::iterator da_concurrent_vector< T >::insert(da_concurrent_vector< T >::const_iterator pos, const T& value)
153{
154 unique_lock_type lg(m_mutex);
155
156 return (m_inner_list.insert(pos, value));
157}
158
159template< typename T >
160void da_concurrent_vector< T >::insert(da_concurrent_vector< T >::iterator pos, da_concurrent_vector< T >::size_type count, const T& value)
161{
162 unique_lock_type lg(m_mutex);
163
164 m_inner_list.insert(pos, count, value);
165}
166
167template< typename T >
168da_concurrent_vector< T >::iterator da_concurrent_vector< T >::insert(da_concurrent_vector< T >::const_iterator pos,
169 da_concurrent_vector< T >::size_type count,
170 const T& value)
171{
172 unique_lock_type lg(m_mutex);
173
174 return (m_inner_list.insert(pos, count, value));
175}
176
177template< typename T >
178void da_concurrent_vector< T >::push_back(const T& value)
179{
180 unique_lock_type lg(m_mutex);
181
182 m_inner_list.push_back(value);
183}
184
185template< typename T >
186void da_concurrent_vector< T >::push_front(const T& value)
187{
188 unique_lock_type lg(m_mutex);
189
190 m_inner_list.push_front(value);
191}
192
193template< typename T >
195{
196 unique_lock_type lg(m_mutex);
197
198 m_inner_list.pop_front();
199}
200
201template< typename T >
203{
204 unique_lock_type lg(m_mutex);
205
206 m_inner_list.pop_back();
207}
208
209template< typename T >
210da_concurrent_vector< T >::iterator da_concurrent_vector< T >::erase(da_concurrent_vector< T >::iterator pos)
211{
212 unique_lock_type lg(m_mutex);
213
214 return (m_inner_list.erase(pos));
215}
216
217template< typename T >
218da_concurrent_vector< T >::iterator da_concurrent_vector< T >::erase(da_concurrent_vector< T >::const_iterator pos)
219{
220 unique_lock_type lg(m_mutex);
221
222 return (m_inner_list.erase(pos));
223}
224
225template< typename T >
226da_concurrent_vector< T >::iterator da_concurrent_vector< T >::erase(da_concurrent_vector< T >::iterator first,
227 da_concurrent_vector< T >::iterator last)
228{
229 unique_lock_type lg(m_mutex);
230
231 return (m_inner_list.erase(first, last));
232}
233
234template< typename T >
235da_concurrent_vector< T >::iterator da_concurrent_vector< T >::erase(da_concurrent_vector< T >::const_iterator first,
236 da_concurrent_vector< T >::const_iterator last)
237{
238 unique_lock_type lg(m_mutex);
239
240 return (m_inner_list.erase(first, last));
241}
242
243template< typename T >
244da_concurrent_vector< T >::iterator da_concurrent_vector< T >::begin()
245{
246 unique_lock_type lg(m_mutex);
247
248 return (m_inner_list.begin());
249}
250
251template< typename T >
252da_concurrent_vector< T >::const_iterator da_concurrent_vector< T >::begin() const
253{
254 unique_lock_type lg(m_mutex);
255
256 return (m_inner_list.begin());
257}
258
259template< typename T >
260da_concurrent_vector< T >::iterator da_concurrent_vector< T >::end()
261{
262 unique_lock_type lg(m_mutex);
263
264 return (m_inner_list.end());
265}
266
267template< typename T >
268da_concurrent_vector< T >::const_iterator da_concurrent_vector< T >::end() const
269{
270 unique_lock_type lg(m_mutex);
271
272 return (m_inner_list.end());
273}
274
275#endif // gree_concurrent_vector_HPP
线程安全的list封装
Definition da_concurrent_vector.hpp:20