![]() |
MySQL 26.7.0
Source Code Documentation
|
Bounded concurrent queue supporting multiple producers and consumers. More...
#include <sync_bounded_queue.h>
Public Types | |
| using | Element_type = T |
| using | Allocator = mysql::allocators::Allocator< T > |
| using | Memory_resource = mysql::allocators::Memory_resource |
| using | Mutex_type = std::mutex |
| using | Cv_type = std::condition_variable |
Public Member Functions | |
| Sync_bounded_queue (Memory_resource={}) | |
| Construct the queue. More... | |
| template<typename P > | |
| bool | enqueue (Element_type &&element, P &&stop_predicate) |
| Enqueue (push) an element into the queue. More... | |
| bool | enqueue (Element_type &&element) |
| Overload without stop_predicate, using default that always returns false. More... | |
| template<typename P > | |
| std::pair< Element_type, bool > | dequeue (P &&stop_predicate) |
| Dequeue an element from the queue. More... | |
| bool | empty () |
| Check if the queue is empty. More... | |
| void | notify_all () |
| Signal shutdown: Notifies all blocked consumers. More... | |
| virtual | ~Sync_bounded_queue ()=default |
| Destructor Before deleting the queue, the caller must ensure that no thread is or will be using the queue, for instance, by first calling notify_all and then joining all consumer threads. More... | |
| std::size_t | size () const |
| Estimates the queue size based on m_head and m_tail positions. More... | |
| void | reset () |
| Reset the queue: Clears all slots (sets validity=false, elements to default), resets m_head and m_tail to 0. More... | |
Static Public Attributes | |
| static constexpr uint64_t | m_capacity = capacity_tp |
Private Member Functions | |
| std::size_t | get_next_index (std::atomic< std::size_t > ¤t) |
| Atomically increments the given position (m_head for producers, m_tail for consumers) and returns the new index modulo capacity. More... | |
Private Attributes | |
| std::array< detail::Padded_slot< Element_type, Mutex_type, Cv_type >, capacity_tp > | m_slots |
| Padded slots containing elements and synchronization primitives. More... | |
| std::atomic< std::size_t > | m_head {0} |
| Current head idx, when at the end, wraps to 0. More... | |
| std::atomic< std::size_t > | m_tail {0} |
| Current tail idx, when at the end, wraps to 0. More... | |
Bounded concurrent queue supporting multiple producers and consumers.
Designed to minimize contention using lock-free index allocation followed by per-slot locking for safe element access and notification.
—Implementation: The queue uses a fixed-size circular buffer implemented as a std::array of Padded_slot structures. Each slot contains:
Producers:
Consumers:
Indices wrap around using % m_capacity (capacity is power of 2). The queue has fixed capacity; enqueue blocks if full (no resizing). Contention is low if capacity >= num_producers + num_consumers. Index allocation is lock-free, but slot access uses per-slot mutex/CV.
| T | Type of element stored in the queue. |
| capacity_tp | Fixed capacity of the queue (must be power of 2). |
| using mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Allocator = mysql::allocators::Allocator<T> |
| using mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Cv_type = std::condition_variable |
| using mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Element_type = T |
| using mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Memory_resource = mysql::allocators::Memory_resource |
| using mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Mutex_type = std::mutex |
| mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::Sync_bounded_queue | ( | Memory_resource | = {} | ) |
Construct the queue.
The memory_resource parameter is reserved for future use and currently ignored.
|
virtualdefault |
Destructor Before deleting the queue, the caller must ensure that no thread is or will be using the queue, for instance, by first calling notify_all and then joining all consumer threads.
| std::pair< Element_type, bool > mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::dequeue | ( | P && | stop_predicate | ) |
Dequeue an element from the queue.
Called by consumers.
| stop_predicate | Predicate checked after wakeup; if true and still cannot dequeue, stops waiting and returns {default T, false}. |
| bool mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::empty | ( | ) |
Check if the queue is empty.
| bool mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::enqueue | ( | Element_type && | element | ) |
Overload without stop_predicate, using default that always returns false.
| element | Element to enqueue |
| bool mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::enqueue | ( | Element_type && | element, |
| P && | stop_predicate | ||
| ) |
Enqueue (push) an element into the queue.
Blocks the calling producer if the queue is full (no space available).
| element | Element to enqueue |
| stop_predicate | Predicate checked after wakeup; if true and still cannot enqueue, stops waiting and returns {default T, false}. |
|
private |
Atomically increments the given position (m_head for producers, m_tail for consumers) and returns the new index modulo capacity.
Used internally for lock-free index allocation.
| [in,out] | current | Atomic position to advance (m_head or m_tail). |
| void mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::notify_all | ( | ) |
Signal shutdown: Notifies all blocked consumers.
Allows waiting consumers to wake and check stop_predicate to exit gracefully. Producer stop must be handled by the structure user, before calling "notify_all".
| void mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::reset | ( | ) |
Reset the queue: Clears all slots (sets validity=false, elements to default), resets m_head and m_tail to 0.
| std::size_t mysql::concurrency::Sync_bounded_queue< T, capacity_tp >::size | ( | ) | const |
Estimates the queue size based on m_head and m_tail positions.
Due to concurrent modifications, this is an approximation. The actual size may be bigger than the returned value; the difference is at most min(N,M) where N and M are the number of concurrent enqueue/dequeue operations.
|
staticconstexpr |
|
private |
Current head idx, when at the end, wraps to 0.
|
private |
Padded slots containing elements and synchronization primitives.
|
private |
Current tail idx, when at the end, wraps to 0.