Corelink CPP Client
Corelink C++ client library
 
Loading...
Searching...
No Matches
concurrent_queue.hpp
Go to the documentation of this file.
1#pragma once
2
3#if defined(CORELINK_USE_CONCURRENT_QUEUE)
4
9
15namespace corelink
16{
17 namespace utils
18 {
19 namespace containers
20 {
25 template<typename item_type>
26 class CORELINK_EXPORT concurrent_queue
27 : public std::enable_shared_from_this<concurrent_queue<item_type>>
28 {
29 private:
33 using queue_type = std::deque<item_type>;
34
38 queue_type m_queue_container;
43
44 public:
49 = default;
50
55 explicit concurrent_queue(size_t initial_size)
56 {
57 m_queue_container.resize(initial_size);
58 }
59
64 {
65 clear();
66 }
67
73 concurrent_queue<item_type> &push(const item_type &value)
74 {
75 commons::writer_lock scope_lock(m_sync_mutex);
76 m_queue_container.emplace_back(value);
77 return *this;
78 }
79
84 item_type &peek()
85 {
86 commons::reader_lock scope_lock(m_sync_mutex);
87 return m_queue_container.front();
88 }
89
93 inline void pop()
94 {
95 commons::writer_lock scope_lock(m_sync_mutex);
96 if (!m_queue_container.empty())
97 {
98 m_queue_container.pop_front();
99 }
100 }
101
107 {
108 commons::writer_lock scope_lock(m_sync_mutex);
109 m_queue_container.clear();
110 return *this;
111 }
112
118 inline size_t size()
119 {
120 commons::reader_lock scoped_lock(m_sync_mutex);
121 return m_queue_container.size();
122 }
123
128 inline bool empty()
129 {
130 commons::reader_lock scoped_lock(m_sync_mutex);
131 return m_queue_container.empty();
132 }
133 };
134 }
135 }
136}
137#endif //CORELINK_USE_CONCURRENT_QUEUE