cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
concurrent_queue.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono> // For std::chrono::microseconds
4#include <cstddef> // For size_t
5#include <functional> // Needed for std::function template instantiation
6#include <memory> // For std::unique_ptr
7#include <optional>
8
10// Assuming export macros are defined elsewhere if needed
11#include <cpp-toolbox/cpp-toolbox_export.hpp>
12// Assuming disable copy/move macros are defined elsewhere
13#include <cpp-toolbox/macro.hpp>
14
16{
17
55template<typename T>
56class CPP_TOOLBOX_EXPORT concurrent_queue_t
57{
58public:
63
71
72 // --- 标准队列操作/Standard Queue Operations ---
73
79 void enqueue(T&& value);
80
90 bool try_dequeue(T& item);
91
101 std::optional<T> try_dequeue();
102
113 bool wait_dequeue_timed(T& item, std::chrono::microseconds timeout);
114
115 // --- 额外工具函数/Additional Utility Functions ---
116
124 size_t size_approx() const;
125
126 // --- 资源管理/Resource Management ---
127
128 // 为简化起见禁用拷贝和移动/Disable copying and moving for simplicity
129 // 如果需要移动语义,移动构造函数/赋值运算符必须在cpp文件中定义/If move
130 // semantics are desired, the move constructor/assignment must also be defined
131 // in the .cpp file
133
134private:
135 // 实现类的前向声明(PIMPL)/Forward declaration of the implementation class
136 // (Pimpl)
137 class Impl;
138
139 // 指向实现细节的指针/Pointer to the implementation details
140 // std::unique_ptr要求Impl在调用析构函数的地方定义/std::unique_ptr requires
141 // Impl to be defined where the destructor is called
142 std::unique_ptr<Impl> impl_;
143};
144
145#if defined(CPP_TOOLBOX_COMPILER_MSVC)
146extern template class concurrent_queue_t<
147 std::unique_ptr<toolbox::base::detail::task_base,
148 std::default_delete<toolbox::base::detail::task_base>>>;
149extern template class concurrent_queue_t<std::function<void()>>;
150
151#else
152extern template class CPP_TOOLBOX_EXPORT concurrent_queue_t<
153 std::unique_ptr<toolbox::base::detail::task_base,
154 std::default_delete<toolbox::base::detail::task_base>>>;
155extern template class CPP_TOOLBOX_EXPORT
156 concurrent_queue_t<std::function<void()>>;
157// extern template class /* CPP_TOOLBOX_EXPORT */
158// concurrent_queue_t<
159// std::pair<toolbox::logger::thread_logger_t::Level, std::string>>;
160#endif
161} // namespace toolbox::container
#define CPP_TOOLBOX_DISABLE_COPY_AND_MOVE(ClassType)
禁用类的拷贝和移动操作 / Disable both copy and move operations
Definition class.hpp:47
高性能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
std::optional< T > try_dequeue()
尝试出队一个元素,以std::optional形式返回(非阻塞)/Attempts to dequeue an item, returning it in an std::optional (non-b...
bool try_dequeue(T &item)
尝试从队列中出队一个元素(非阻塞)/Attempts to dequeue an item from the queue (non-blocking)
concurrent_queue_t()
构造队列包装器/Constructs the queue wrapper
~concurrent_queue_t()
销毁队列包装器并清理资源/Destroys the queue wrapper and cleans up resources
size_t size_approx() const
返回队列中元素的近似数量/Returns an approximate count of items in the queue
void enqueue(T &&value)
将元素入队/Enqueues an item into the queue
通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macro...
Definition concurrent_queue.hpp:16
Definition task_base.hpp:9