|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Thread-safe MPMC queue with blocking wait support (Internal implementation) More...
#include <concurrent_queue.h>

Classes | |
| struct | node |
Public Member Functions | |
| concurrent_queue () | |
| Constructs an empty queue. | |
| ~concurrent_queue () | |
| Destructor - signals shutdown and drains the queue. | |
| concurrent_queue (const concurrent_queue &)=delete | |
| concurrent_queue & | operator= (const concurrent_queue &)=delete |
| concurrent_queue (concurrent_queue &&)=delete | |
| concurrent_queue & | operator= (concurrent_queue &&)=delete |
| void | enqueue (T value) |
| Enqueues a value into the queue. | |
| auto | try_dequeue () -> std::optional< T > |
| Tries to dequeue a value (non-blocking) | |
| template<typename Rep , typename Period > | |
| auto | wait_dequeue (const std::chrono::duration< Rep, Period > &timeout) -> std::optional< T > |
| Dequeues a value with blocking wait. | |
| auto | wait_dequeue () -> std::optional< T > |
| Dequeues a value with indefinite blocking wait. | |
| auto | empty () const noexcept -> bool |
| Checks if the queue appears empty. | |
| auto | size () const noexcept -> std::size_t |
| Gets approximate queue size. | |
| void | notify_one () |
| Wakes one waiting consumer. | |
| void | notify_all () |
| Wakes all waiting consumers. | |
| void | shutdown () |
| Signals shutdown and wakes all waiters. | |
| auto | is_shutdown () const noexcept -> bool |
| Checks if shutdown has been signaled. | |
Private Attributes | |
| node * | head_ |
| node * | tail_ |
| std::mutex | head_mutex_ |
| std::mutex | tail_mutex_ |
| std::atomic< std::size_t > | size_ {0} |
| std::atomic< bool > | shutdown_ {false} |
| std::mutex | cv_mutex_ |
| std::condition_variable | cv_ |
Thread-safe MPMC queue with blocking wait support (Internal implementation)
This class implements a thread-safe Multi-Producer Multi-Consumer (MPMC) queue using fine-grained locking for simplicity and correctness. It provides both non-blocking and blocking operations for flexible use cases.
This implementation uses fine-grained locking with separate head and tail mutexes rather than a true lock-free algorithm. This provides:
While a true lock-free Michael-Scott queue requires complex memory reclamation (Hazard Pointers or epoch-based reclamation), fine-grained locking offers comparable performance for most practical scenarios.
This class is intentionally named "concurrent_queue" (not "lockfree_queue") because:
For a true lock-free queue implementation, see lockfree_job_queue which uses the Michael-Scott algorithm with hazard pointers for memory reclamation.
| T | The element type (must be move-constructible) |
|
inline |
Constructs an empty queue.
Initializes the queue with a dummy node to simplify the algorithm.
Definition at line 100 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::head_, and kcenon::thread::detail::concurrent_queue< T >::tail_.
|
inline |
Destructor - signals shutdown and drains the queue.
Definition at line 109 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::head_, kcenon::thread::detail::concurrent_queue< T >::shutdown(), and kcenon::thread::detail::concurrent_queue< T >::try_dequeue().

|
delete |
|
delete |
|
inlinenodiscardnoexcept |
Checks if the queue appears empty.
Definition at line 231 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::size_.
|
inline |
Enqueues a value into the queue.
| value | The value to enqueue (moved) |
Definition at line 131 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::node::next, kcenon::thread::detail::concurrent_queue< T >::notify_one(), kcenon::thread::detail::concurrent_queue< T >::shutdown_, kcenon::thread::detail::concurrent_queue< T >::size_, kcenon::thread::detail::concurrent_queue< T >::tail_, and kcenon::thread::detail::concurrent_queue< T >::tail_mutex_.

|
inlinenodiscardnoexcept |
Checks if shutdown has been signaled.
Definition at line 269 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::shutdown_.
|
inline |
Wakes all waiting consumers.
Definition at line 253 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::cv_, and kcenon::thread::detail::concurrent_queue< T >::cv_mutex_.
Referenced by kcenon::thread::detail::concurrent_queue< T >::shutdown().

|
inline |
Wakes one waiting consumer.
Definition at line 245 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::cv_, and kcenon::thread::detail::concurrent_queue< T >::cv_mutex_.
Referenced by kcenon::thread::detail::concurrent_queue< T >::enqueue().

|
delete |
|
delete |
|
inline |
Signals shutdown and wakes all waiters.
Definition at line 261 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::notify_all(), and kcenon::thread::detail::concurrent_queue< T >::shutdown_.
Referenced by kcenon::thread::detail::concurrent_queue< T >::~concurrent_queue().


|
inlinenodiscardnoexcept |
Gets approximate queue size.
Definition at line 238 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::size_.
|
inlinenodiscard |
Tries to dequeue a value (non-blocking)
Definition at line 155 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::node::data, kcenon::thread::detail::concurrent_queue< T >::head_, kcenon::thread::detail::concurrent_queue< T >::head_mutex_, kcenon::thread::detail::concurrent_queue< T >::node::next, and kcenon::thread::detail::concurrent_queue< T >::size_.
Referenced by kcenon::thread::detail::concurrent_queue< T >::wait_dequeue(), and kcenon::thread::detail::concurrent_queue< T >::~concurrent_queue().

|
inlinenodiscard |
Dequeues a value with indefinite blocking wait.
Definition at line 224 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::wait_dequeue().
Referenced by kcenon::thread::detail::concurrent_queue< T >::wait_dequeue().


|
inlinenodiscard |
Dequeues a value with blocking wait.
| timeout | Maximum time to wait |
Definition at line 187 of file concurrent_queue.h.
References kcenon::thread::detail::concurrent_queue< T >::cv_, kcenon::thread::detail::concurrent_queue< T >::cv_mutex_, kcenon::thread::detail::concurrent_queue< T >::shutdown_, and kcenon::thread::detail::concurrent_queue< T >::try_dequeue().

|
private |
Definition at line 293 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::notify_all(), kcenon::thread::detail::concurrent_queue< T >::notify_one(), and kcenon::thread::detail::concurrent_queue< T >::wait_dequeue().
|
mutableprivate |
Definition at line 292 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::notify_all(), kcenon::thread::detail::concurrent_queue< T >::notify_one(), and kcenon::thread::detail::concurrent_queue< T >::wait_dequeue().
|
private |
Definition at line 282 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::concurrent_queue(), kcenon::thread::detail::concurrent_queue< T >::try_dequeue(), and kcenon::thread::detail::concurrent_queue< T >::~concurrent_queue().
|
mutableprivate |
Definition at line 285 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::try_dequeue().
|
private |
Definition at line 289 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::enqueue(), kcenon::thread::detail::concurrent_queue< T >::is_shutdown(), kcenon::thread::detail::concurrent_queue< T >::shutdown(), and kcenon::thread::detail::concurrent_queue< T >::wait_dequeue().
|
private |
Definition at line 288 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::empty(), kcenon::thread::detail::concurrent_queue< T >::enqueue(), kcenon::thread::detail::concurrent_queue< T >::size(), and kcenon::thread::detail::concurrent_queue< T >::try_dequeue().
|
private |
Definition at line 283 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::concurrent_queue(), and kcenon::thread::detail::concurrent_queue< T >::enqueue().
|
mutableprivate |
Definition at line 286 of file concurrent_queue.h.
Referenced by kcenon::thread::detail::concurrent_queue< T >::enqueue().