Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::LFQueue< T, Alloc > Class Template Reference

基于自旋的高性能 FIFO 队列,支持多生产者/多消费者并发。 More...

#include <queue.h>

Inheritance diagram for console::LFQueue< T, Alloc >:

Classes

struct  Node

Public Member Functions

 LFQueue ()
 默认构造函数,使用默认分配器初始化队列。
 LFQueue (const Alloc &alloc)
 使用指定分配器初始化队列。
 LFQueue (Alloc &&alloc)
 使用移动分配器初始化队列。
 ~LFQueue ()
 析构函数,销毁队列中的所有节点。
void push (const T &data)
 将数据推送到队列中。
void push (T &&data)
 将数据推送到队列中。
template<class Iterator>
void push (Iterator begin, size_t count)
 将大批数据推送到队列中。
template<class Iterator>
void push (Iterator begin, Iterator end)
 将大批数据推送到队列中。
bool pop (T &output)
 从队列中弹出数据。
std::unique_ptr< T > pop ()
 从队列中弹出数据,并返回一个独占所有权的 std::unique_ptr。
template<class Iterator>
size_t pop (Iterator output, size_t count)
 从队列中弹出数据大批数据,写入输出迭代器。
template<class Iterator>
size_t pop (Iterator begin, Iterator end)
 从队列中弹出数据大批数据,写入输出迭代器。
std::vector< T > pop (size_t count)
 从队列中弹出数据大批数据,返回包含弹出结果的 vector。

Private Types

using NodeAlloc
 节点分配器类型。
using NodeTraits = std::allocator_traits<NodeAlloc>
 节点分配器的特征类型。

Private Member Functions

Alloc & get_alloc ()
 获取分配器的引用。
Node * create_node (const T &data)
 创建一个新的节点。
void destroy_node (Node *node)
 销毁节点。

Private Attributes

std::atomic< Node * > head_
 指向队列头部的原子指针
std::atomic< Node * > tail_
 指向队列尾部的原子指针

Detailed Description

template<class T, class Alloc = std::allocator<T>>
class console::LFQueue< T, Alloc >

基于自旋的高性能 FIFO 队列,支持多生产者/多消费者并发。

Template Parameters
T队列中元素的类型。
Alloc分配器类型,默认为 std::allocator<T>。
Note
通过原子 exchange 实现轻量级互斥,临界区极短 (~3-10ns)。 提供 Lock-Free 进度保证 (至少一个线程能前进),但不是 Wait-Free。

Member Typedef Documentation

◆ NodeAlloc

template<class T, class Alloc = std::allocator<T>>
using console::LFQueue< T, Alloc >::NodeAlloc
private
Initial value:
typename std::allocator_traits<Alloc>::template rebind_alloc<Node>

节点分配器类型。

◆ NodeTraits

template<class T, class Alloc = std::allocator<T>>
using console::LFQueue< T, Alloc >::NodeTraits = std::allocator_traits<NodeAlloc>
private

节点分配器的特征类型。

Constructor & Destructor Documentation

◆ LFQueue() [1/3]

template<class T, class Alloc = std::allocator<T>>
console::LFQueue< T, Alloc >::LFQueue ( )
inline

默认构造函数,使用默认分配器初始化队列。

◆ LFQueue() [2/3]

template<class T, class Alloc = std::allocator<T>>
console::LFQueue< T, Alloc >::LFQueue ( const Alloc & alloc)
inline

使用指定分配器初始化队列。

Parameters
alloc分配器对象。

◆ LFQueue() [3/3]

template<class T, class Alloc = std::allocator<T>>
console::LFQueue< T, Alloc >::LFQueue ( Alloc && alloc)
inline

使用移动分配器初始化队列。

Parameters
alloc分配器对象。

◆ ~LFQueue()

template<class T, class Alloc = std::allocator<T>>
console::LFQueue< T, Alloc >::~LFQueue ( )
inline

析构函数,销毁队列中的所有节点。

Note
首先独占 head_ 和 tail_ 资源,然后销毁所有节点。
Warning
析构后的任何操作都会永久阻塞,直到队列被重新初始化, 这是合理的,因为用户有确保队列析构后不在被使用的义务。

Member Function Documentation

◆ create_node()

template<class T, class Alloc = std::allocator<T>>
Node * console::LFQueue< T, Alloc >::create_node ( const T & data)
inlineprivate

创建一个新的节点。

◆ destroy_node()

template<class T, class Alloc = std::allocator<T>>
void console::LFQueue< T, Alloc >::destroy_node ( Node * node)
inlineprivate

销毁节点。

◆ get_alloc()

template<class T, class Alloc = std::allocator<T>>
Alloc & console::LFQueue< T, Alloc >::get_alloc ( )
inlineprivate

获取分配器的引用。

◆ pop() [1/5]

template<class T, class Alloc = std::allocator<T>>
std::unique_ptr< T > console::LFQueue< T, Alloc >::pop ( )
inline

从队列中弹出数据,并返回一个独占所有权的 std::unique_ptr。

Returns
如果成功弹出数据,则返回一个包含数据的 std::unique_ptr;否则返回 nullptr。
Note
将实际工作委托至 pop(T &)。

◆ pop() [2/5]

template<class T, class Alloc = std::allocator<T>>
template<class Iterator>
size_t console::LFQueue< T, Alloc >::pop ( Iterator begin,
Iterator end )
inline

从队列中弹出数据大批数据,写入输出迭代器。

Parameters
begin输出迭代器的起始位置,用于写入弹出的数据。
end输出迭代器的结束位置。
Returns
实际弹出的数据数量。
Note
将实际工作委托至 pop(Iterator, size_t)。

◆ pop() [3/5]

template<class T, class Alloc = std::allocator<T>>
template<class Iterator>
size_t console::LFQueue< T, Alloc >::pop ( Iterator output,
size_t count )
inline

从队列中弹出数据大批数据,写入输出迭代器。

Parameters
output输出迭代器,用于写入弹出的数据。
count要弹出的数据数量。
Returns
实际弹出的数据数量。

◆ pop() [4/5]

template<class T, class Alloc = std::allocator<T>>
std::vector< T > console::LFQueue< T, Alloc >::pop ( size_t count)
inline

从队列中弹出数据大批数据,返回包含弹出结果的 vector。

Parameters
count要弹出的数据数量。
Returns
实际弹出的数据,通过 .size() 获取数据数量。
Note
将实际工作委托至 pop(Iterator, size_t)。

◆ pop() [5/5]

template<class T, class Alloc = std::allocator<T>>
bool console::LFQueue< T, Alloc >::pop ( T & output)
inline

从队列中弹出数据。

Parameters
output弹出的数据将存储在此处。
Returns
如果成功弹出数据,则返回 true;否则返回 false。
Note
首先独占 head_ 资源,然后获取队列头部的下一个节点。 如果没有下一个节点,则释放 head_ 资源,返回 false。 否则,将数据移动到输出参数中,并释放 head_ 资源,返回 true。

◆ push() [1/4]

template<class T, class Alloc = std::allocator<T>>
void console::LFQueue< T, Alloc >::push ( const T & data)
inline

将数据推送到队列中。

Parameters
data要推送的数据。
Note
首先独占 tail_ 资源,然后将新节点链接到队列尾部。 然后释放 tail_ 资源,允许其他线程访问队列。

◆ push() [2/4]

template<class T, class Alloc = std::allocator<T>>
template<class Iterator>
void console::LFQueue< T, Alloc >::push ( Iterator begin,
Iterator end )
inline

将大批数据推送到队列中。

Parameters
begin数据迭代器的起始位置。
end数据迭代器的结束位置。
Note
将实际工作委托至 push(Iterator, size_t)。

◆ push() [3/4]

template<class T, class Alloc = std::allocator<T>>
template<class Iterator>
void console::LFQueue< T, Alloc >::push ( Iterator begin,
size_t count )
inline

将大批数据推送到队列中。

Parameters
begin数据迭代器的起始位置。
count要推送的数据数量。
Note
提前批量构造所有节点,然后一次性链接为完整链表并挂载至队尾, 从而将临界区压缩为两次原子操作,显著降低多线程竞争下的自旋开销。 若构造过程中抛出异常,会先销毁已构造的节点再抛出,保证不会泄漏内存。 相比逐元素 push 减少了对 tail_ 的独占/释放次数,批量场景下吞吐更高。

◆ push() [4/4]

template<class T, class Alloc = std::allocator<T>>
void console::LFQueue< T, Alloc >::push ( T && data)
inline

将数据推送到队列中。

Parameters
data要推送的数据。
Note
首先独占 tail_ 资源,然后将新节点链接到队列尾部。 然后释放 tail_ 资源,允许其他线程访问队列。

Member Data Documentation

◆ head_

template<class T, class Alloc = std::allocator<T>>
std::atomic<Node *> console::LFQueue< T, Alloc >::head_
private

指向队列头部的原子指针

◆ tail_

template<class T, class Alloc = std::allocator<T>>
std::atomic<Node *> console::LFQueue< T, Alloc >::tail_
private

指向队列尾部的原子指针


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