cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::container::concurrent_queue_t< T > Class Template Reference

高性能MPMC并发队列的包装器/A wrapper around a high-performance MPMC concurrent queue More...

#include <concurrent_queue.hpp>

Public Member Functions

 concurrent_queue_t ()
 构造队列包装器/Constructs the queue wrapper
 
 ~concurrent_queue_t ()
 销毁队列包装器并清理资源/Destroys the queue wrapper and cleans up resources
 
void enqueue (T &&value)
 将元素入队/Enqueues an item into the queue
 
bool try_dequeue (T &item)
 尝试从队列中出队一个元素(非阻塞)/Attempts to dequeue an item from the queue (non-blocking)
 
std::optional< T > try_dequeue ()
 尝试出队一个元素,以std::optional形式返回(非阻塞)/Attempts to dequeue an item, returning it in an std::optional (non-blocking)
 
bool wait_dequeue_timed (T &item, std::chrono::microseconds timeout)
 尝试出队一个元素,阻塞直到有元素可用或超时/Attempts to dequeue an item, blocking until an item is available or timeout
 
size_t size_approx () const
 返回队列中元素的近似数量/Returns an approximate count of items in the queue
 

Detailed Description

template<typename T>
class toolbox::container::concurrent_queue_t< T >

高性能MPMC并发队列的包装器/A wrapper around a high-performance MPMC concurrent queue

该类使用PIMPL惯用法隐藏底层第三方库实现细节,同时提供稳定的接口/This class provides a stable interface while hiding the underlying third-party library implementation details using the Pimpl idiom

Template Parameters
T队列中存储的元素类型,必须是可移动的/The type of elements stored in the queue. Must be movable
// 创建一个整数类型的并发队列/Create a concurrent queue for integers
// 生产者线程/Producer thread
queue.enqueue(42);
// 消费者线程 - 使用引用方式/Consumer thread - using reference
int value;
if(queue.try_dequeue(value)) {
// 处理值/Process value
}
// 消费者线程 - 使用optional方式/Consumer thread - using optional
if(auto opt = queue.try_dequeue()) {
int value = *opt;
// 处理值/Process value
}
// 带超时的消费/Timed consumption
int value;
if(queue.wait_dequeue_timed(value, std::chrono::microseconds(1000))) {
// 在1ms内成功获取到值/Successfully got value within 1ms
}
高性能MPMC并发队列的包装器/A wrapper around a high-performance MPMC concurrent queue
Definition concurrent_queue.hpp:57
bool wait_dequeue_timed(T &item, std::chrono::microseconds timeout)
尝试出队一个元素,阻塞直到有元素可用或超时/Attempts to dequeue an item, blocking until an item is available or timeout
bool try_dequeue(T &item)
尝试从队列中出队一个元素(非阻塞)/Attempts to dequeue an item from the queue (non-blocking)
void enqueue(T &&value)
将元素入队/Enqueues an item into the queue

Constructor & Destructor Documentation

◆ concurrent_queue_t()

template<typename T >
toolbox::container::concurrent_queue_t< T >::concurrent_queue_t ( )

构造队列包装器/Constructs the queue wrapper

◆ ~concurrent_queue_t()

销毁队列包装器并清理资源/Destroys the queue wrapper and cleans up resources

Note
由于使用了PIMPL惯用法和unique_ptr,必须在cpp文件中定义/Must be defined in the .cpp file due to Pimpl idiom with unique_ptr

Member Function Documentation

◆ enqueue()

template<typename T >
void toolbox::container::concurrent_queue_t< T >::enqueue ( T &&  value)

将元素入队/Enqueues an item into the queue

支持多生产者线程安全/Thread-safe for multiple producers

Parameters
value要入队的值(将被移动)/The value to enqueue (will be moved)

◆ size_approx()

template<typename T >
size_t toolbox::container::concurrent_queue_t< T >::size_approx ( ) const

返回队列中元素的近似数量/Returns an approximate count of items in the queue

适用于启发式估计,在高并发场景下可能不精确/Useful for heuristics but may not be exact in a highly concurrent scenario

Returns
队列中元素的近似数量/Approximate number of items in the queue

◆ try_dequeue() [1/2]

template<typename T >
std::optional< T > toolbox::container::concurrent_queue_t< T >::try_dequeue ( )

尝试出队一个元素,以std::optional形式返回(非阻塞)/Attempts to dequeue an item, returning it in an std::optional (non-blocking)

支持多消费者线程安全/Thread-safe for multiple consumers

Returns
如果成功包含出队值的std::optional<T>,否则返回std::nullopt/std::optional<T> containing the dequeued value if successful, or std::nullopt if the queue was empty

◆ try_dequeue() [2/2]

template<typename T >
bool toolbox::container::concurrent_queue_t< T >::try_dequeue ( T &  item)

尝试从队列中出队一个元素(非阻塞)/Attempts to dequeue an item from the queue (non-blocking)

支持多消费者线程安全/Thread-safe for multiple consumers

Parameters
[out]item如果成功,用于存储出队值的引用/Reference to store the dequeued value if successful
Returns
如果成功出队返回true,队列为空返回false/True if an item was successfully dequeued, false if the queue was empty

◆ wait_dequeue_timed()

template<typename T >
bool toolbox::container::concurrent_queue_t< T >::wait_dequeue_timed ( T &  item,
std::chrono::microseconds  timeout 
)

尝试出队一个元素,阻塞直到有元素可用或超时/Attempts to dequeue an item, blocking until an item is available or timeout

支持多消费者线程安全/Thread-safe for multiple consumers

Parameters
[out]item如果成功,用于存储出队值的引用/Reference to store the dequeued value if successful
timeout最大等待时间/The maximum duration to wait
Returns
如果在超时前成功出队返回true,否则返回false/True if an item was successfully dequeued within the timeout, false otherwise

The documentation for this class was generated from the following file: