-
Notifications
You must be signed in to change notification settings - Fork 1
/
thread_safe_deque.h
101 lines (70 loc) · 5.72 KB
/
thread_safe_deque.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef THREAD_SAFE_DEQUE_H_INCLUDED
#define THREAD_SAFE_DEQUE_H_INCLUDED
#include <deque>
#include <boost/thread.hpp>
namespace thread_safe {
template < class T, class Allocator = std::allocator<T> >
class deque {
public:
typedef typename std::deque<T, Allocator>::iterator iterator;
typedef typename std::deque<T, Allocator>::const_iterator const_iterator;
typedef typename std::deque<T, Allocator>::reverse_iterator reverse_iterator;
typedef typename std::deque<T, Allocator>::const_reverse_iterator const_reverse_iterator;
typedef typename std::deque<T, Allocator>::allocator_type allocator_type;
typedef typename std::deque<T, Allocator>::value_type value_type;
typedef typename std::deque<T, Allocator>::size_type size_type;
typedef typename std::deque<T, Allocator>::difference_type difference_type;
//Constructors
explicit deque( const Allocator & alloc = Allocator() ) : storage( alloc ) { }
explicit deque( size_type n, const T & value = T(), const Allocator & alloc = Allocator() ) : storage( n, value, alloc ) { }
template <class InputIterator> deque( InputIterator first, InputIterator last, const Allocator & alloc = Allocator() ) : storage( first, last, alloc ) { }
deque( const thread_safe::deque<T, Allocator> & x ) { boost::lock_guard<boost::mutex> lock( x.mutex ); storage = x.storage; }
//Copy
thread_safe::deque<T,Allocator>& operator=( const thread_safe::deque<T,Allocator>& x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage = x.storage; return *this;}
//Destructor
~deque<T,Allocator>( void ) { }
//Iterators
iterator begin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
const_iterator begin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.begin(); }
iterator end( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
const_iterator end( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.end(); }
reverse_iterator rbegin( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
const_reverse_iterator rbegin( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rbegin(); }
reverse_iterator rend( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
const_reverse_iterator rend( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.rend(); }
//Capacity
size_type size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.size(); }
size_type max_size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.max_size(); }
void resize( size_type n, T c = T() ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.resize( n, c ); }
bool empty( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.empty(); }
//Element access
T & operator[]( size_type n ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage[n]; }
const T & operator[]( size_type n ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage[n]; }
T & at( size_type n ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.at( n ); }
const T & at( size_type n ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.at( n ); }
T & front( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.front(); }
const T & front( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.back(); }
T & back( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.back(); }
const T & back( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.back(); }
//Modifiers
void assign( size_type n, T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.assign( n, u ); }
template <class InputIterator> void assign( InputIterator begin, InputIterator end ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.assign( begin, end ); }
void push_back( const T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.push_back( u ); }
void pop_back( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.pop_back(); }
void push_front( const T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.push_front( u ); }
void pop_front( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.pop_front(); }
iterator insert( iterator pos, const T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.insert( pos, u ); }
void insert( iterator pos, size_type n, const T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.insert( pos, n, u ); }
template <class InputIterator> void insert( iterator pos, InputIterator begin, InputIterator end ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.insert( pos, begin, end ); }
void erase( iterator pos ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( pos ); }
void erase( iterator begin, iterator end ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.erase( begin, end ); }
void swap( thread_safe::deque<T, Allocator> & x ) { boost::lock_guard<boost::mutex> lock( mutex ); boost::lock_guard<boost::mutex> lock2( x.mutex ); storage.swap( x.storage ); }
void clear( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.clear(); }
//Allocator
allocator_type get_allocator( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.get_allocator(); }
private:
std::deque<T, Allocator> storage;
mutable boost::mutex mutex;
};
}
#endif // THREAD_SAFE_DEQUE_H_INCLUDED