Queue
1.0.0
A C++17 Library of various `queue` containers
|
Go to the documentation of this file.
48 #ifndef concurrent_queue_h__
49 #define concurrent_queue_h__
56 #include <condition_variable>
60 #include <shared_mutex>
76 template<
class T,
template<
class,
class>
class Queue = std::deque,
class Alloc = std::allocator<T>>
94 typedef typename std::reverse_iterator<
iterator> reverse_iterator;
138 : queue(n, val, alloc)
148 template<
typename InputIterator>
150 : queue(first, last, alloc)
227 std::scoped_lock lock(lock_this, lock_that);
230 new_element.notify_all();
242 std::scoped_lock lock(lock_this, lock_that);
244 queue = std::move(other.queue);
245 new_element.notify_all();
273 template<
class... Args>
277 queue.emplace_back(std::forward<Args>(args)...);
278 new_element.notify_one();
289 return queue.empty();
298 return queue.get_allocator();
305 inline void push(
const T& value)
308 queue.push_back(value);
309 new_element.notify_one();
319 queue.push_back(std::move(value));
320 new_element.notify_one();
354 if (!lock_this.try_lock() || queue.empty())
358 destination = std::move(queue.front());
374 template<
class Rep,
class Period>
375 inline bool try_pop_for(T& destination,
const std::chrono::duration<Rep, Period>& timeout_duration)
377 auto start = std::chrono::steady_clock::now();
381 if (!lock_this.try_lock_for(timeout_duration))
385 while (queue.empty())
387 auto elapsed = std::chrono::steady_clock::now() - start;
388 if (elapsed > timeout_duration || new_element.wait_for(lock_this, timeout_duration - elapsed) == std::cv_status::timeout)
393 destination = std::move(queue.front());
413 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
414 [[
deprecated(
"concurrent_queue::begin() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
419 return queue.begin();
427 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
428 [[
deprecated(
"concurrent_queue::begin() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
433 return queue.cbegin();
441 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
442 [[
deprecated(
"concurrent_queue::cbegin() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
447 return queue.cbegin();
455 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
456 [[
deprecated(
"concurrent_queue::end() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
469 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
470 [[
deprecated(
"concurrent_queue::end() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
483 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
484 [[
deprecated(
"concurrent_queue::cend() is not thread-safe. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
512 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
513 [[
deprecated(
"concurrent_queue::acquire_read_lock() is not recommended for production code. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
526 #ifndef _CONCURRENT_QUEUE_NO_WARNINGS
527 [[
deprecated(
"concurrent_queue::acquire_write_lock() is not recommended for production code. Define `_CONCURRENT_QUEUE_NO_WARNINGS` to disable this message")]]
547 template<
class Ty,
template<
class,
class>
class Q,
class A>
553 template<
class Ty,
template<
class,
class>
class Q,
class A>
568 template<
class Ty,
template<
class,
class>
class Q,
class A>
593 : queue(other.queue, alloc)
602 : queue(std::move(other.queue))
612 : queue(std::move(other.queue), alloc)
630 template<
class T,
template<
class,
class>
class Queue = std::deque,
class Alloc = std::allocator<T>>
633 typename concurrent_queue<T, Queue, Alloc>::read_lock_type lock_lhs(lhs.mutex, std::defer_lock);
634 typename concurrent_queue<T, Queue, Alloc>::read_lock_type lock_rhs(rhs.mutex, std::defer_lock);
635 std::scoped_lock lock(lock_lhs, lock_rhs);
637 if (lhs.queue.size() != rhs.queue.size())
640 for (
int i = 0; i < lhs.queue.size(); ++i)
641 if (lhs.queue[i] != rhs.queue[i])
649 template<
class T,
template<
class,
class>
class Queue = std::deque,
class Alloc = std::allocator<T>>
652 return !(lhs == rhs);
658 template<
class T,
template<
class,
class>
class Queue = std::deque,
class Alloc = std::allocator<T>>
663 typename concurrent_queue<T, Queue, Alloc>::read_lock_type lock_lhs(lhs.mutex, std::defer_lock);
664 typename concurrent_queue<T, Queue, Alloc>::read_lock_type lock_rhs(rhs.mutex, std::defer_lock);
665 std::scoped_lock lock(lock_lhs, lock_rhs);
667 std::swap(lhs.queue, rhs.queue);
concurrent_queue(const concurrent_queue &other, const allocator_type &alloc)
Copy Constructor with allocator.
Definition: concurrent_queue.h:166
size_t size() const
Returns the number of items in the queue.
Definition: concurrent_queue.h:330
allocator_type get_allocator() const noexcept
Returns a copy of the allocator used to construct the concurrent queue.
Definition: concurrent_queue.h:295
iterator end()
Returns an iterator of type iterator to the end of the concurrent queue.
Definition: concurrent_queue.h:459
read_lock_type acquire_read_lock() const
Lock the queue in a manner in which it is safe for multiple threads to read-iterate over it.
Definition: concurrent_queue.h:516
concurrent_queue(const concurrent_queue &other)
Copy Constructor.
Definition: concurrent_queue.h:157
bool operator!=(const concurrent_queue< T, Queue, Alloc > &lhs, const concurrent_queue< T, Queue, Alloc > &rhs)
Inequality Operator.
Definition: concurrent_queue.h:650
concurrent_queue & operator=(const concurrent_queue &other)
Copy Assignment Operator.
Definition: concurrent_queue.h:222
queue_type::const_iterator const_iterator
A type that represents a non-thread-safe const iterator over elements in a concurrent queue.
Definition: concurrent_queue.h:93
void push(T &&value)
Enqueues an item at tail end of the concurrent queue.
Definition: concurrent_queue.h:316
size_t size_type
A type that counts the number of elements in a concurrent queue.
Definition: concurrent_queue.h:100
const_iterator end() const
Returns an iterator of type const_iterator to the end of the concurrent queue.
Definition: concurrent_queue.h:473
Queue< T, Alloc > queue_type
A type that represents the underlying non-thread-safe data structure for the concurrent queue.
Definition: concurrent_queue.h:91
concurrent_queue(const allocator_type &alloc)
Default Constructor.
Definition: concurrent_queue.h:118
friend bool operator==(const concurrent_queue< Ty, Q, A > &lhs, const concurrent_queue< Ty, Q, A > &rhs)
Equality Operator.
void push(const T &value)
Enqueues an item at tail end of the concurrent queue.
Definition: concurrent_queue.h:305
bool try_pop_for(T &destination, const std::chrono::duration< Rep, Period > &timeout_duration)
Dequeues an item from the queue if one is available within the specified timeout.
Definition: concurrent_queue.h:375
~concurrent_queue()=default
Destructor.
bool empty() const noexcept
Tests if the concurrent queue is empty at the moment this method is called.
Definition: concurrent_queue.h:286
const_iterator begin() const
Returns an iterator of type const_iterator to the beginning of the concurrent queue.
Definition: concurrent_queue.h:431
std::shared_lock< mutex_type > read_lock_type
A type representing a lock on the concurrent queue's mutex which is sufficient to read data in a thre...
Definition: concurrent_queue.h:97
concurrent_queue(size_type n, const value_type &val, const allocator_type &alloc=allocator_type())
Fill Constructor.
Definition: concurrent_queue.h:137
void emplace(Args &&... args)
Constructs a new element in place at the end of the concurrent queue.
Definition: concurrent_queue.h:274
std::reverse_iterator< const_iterator > const_reverse_iterator
A type that represents a reverse non-thread-safe const iterator over elements in a concurrent queue.
Definition: concurrent_queue.h:95
void swap(concurrent_queue< T, Queue, Alloc > &lhs, concurrent_queue< T, Queue, Alloc > &rhs)
Swap Operator.
Definition: concurrent_queue.h:659
queue_type::iterator iterator
A type that represents a non-thread-safe iterator over the elements in a concurrent queue.
Definition: concurrent_queue.h:92
const_iterator cbegin() const
Returns an iterator of type const_iterator to the beginning of the concurrent queue.
Definition: concurrent_queue.h:445
T value_type
A type that represents the data type stored in a concurrent queue.
Definition: concurrent_queue.h:84
bool operator==(const concurrent_queue< T, Queue, Alloc > &lhs, const concurrent_queue< T, Queue, Alloc > &rhs)
Equality Operator.
Definition: concurrent_queue.h:631
concurrent_queue(std::initializer_list< T > init, const allocator_type &alloc=allocator_type())
Initializer List Constructor.
Definition: concurrent_queue.h:192
write_lock_type acquire_write_lock()
Lock the queue in a manner in which it is safe for multiple threads to write-iterate over it.
Definition: concurrent_queue.h:530
iterator begin()
Returns an iterator of type iterator to the beginning of the concurrent queue.
Definition: concurrent_queue.h:417
const typedef value_type & const_reference
A type that provides a reference to a const element stored in a concurrent queue for reading and perf...
Definition: concurrent_queue.h:88
The concurrent_queue class is a sequence container class that allows first-in, first-out access to it...
Definition: concurrent_queue.h:77
std::allocator_traits< allocator_type >::const_pointer const_pointer
A type that provides a const pointer to an element stored in a concurrent queue.
Definition: concurrent_queue.h:90
Alloc allocator_type
A type that represents the allocator class for the concurrent queue.
Definition: concurrent_queue.h:85
concurrent_queue & operator=(concurrent_queue &&other) noexcept
Move Assignment Operator.
Definition: concurrent_queue.h:237
std::allocator_traits< allocator_type >::pointer pointer
A type that provides a pointer to an element stored in a concurrent queue.
Definition: concurrent_queue.h:89
bool try_pop(T &destination)
Dequeues an item from the queue if one is available.
Definition: concurrent_queue.h:345
concurrent_queue(size_type n, const allocator_type &alloc=allocator_type())
Fill Constructor.
Definition: concurrent_queue.h:127
friend void std::swap(concurrent_queue< Ty, Q, A > &lhs, concurrent_queue< Ty, Q, A > &rhs)
Swap Operator.
std::shared_timed_mutex mutex_type
A type that represents the mutex protecting the concurrent queue.
Definition: concurrent_queue.h:96
std::condition_variable_any condition_type
A type that provides a waitable condition of the concurrent queue.
Definition: concurrent_queue.h:87
const_iterator cend() const
Returns an iterator of type const_iterator to the end of the concurrent queue.
Definition: concurrent_queue.h:487
friend bool operator!=(const concurrent_queue< Ty, Q, A > &lhs, const concurrent_queue< Ty, Q, A > &rhs)
Inequality Operator.
std::unique_lock< mutex_type > write_lock_type
A type representing a lock on the concurrent queue's mutex which is sufficient to write data in a thr...
Definition: concurrent_queue.h:98
std::iterator_traits< iterator >::difference_type difference_type
A type that provides the signed distance between two elements in a concurrent queue.
Definition: concurrent_queue.h:99
concurrent_queue(concurrent_queue &&other, const allocator_type &alloc) noexcept
Move Constructor.
Definition: concurrent_queue.h:184
concurrent_queue()=default
Default Constructor.
value_type & reference
A type that provides a reference to an element stored in a concurrent queue.
Definition: concurrent_queue.h:86
concurrent_queue(InputIterator first, InputIterator last, const allocator_type &alloc=allocator_type())
Range Constructor.
Definition: concurrent_queue.h:149
concurrent_queue(concurrent_queue &&other) noexcept
Move Constructor.
Definition: concurrent_queue.h:175
void clear() noexcept
Clears the concurrent queue, destroying any currently enqueued elements.
Definition: concurrent_queue.h:262