cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
thread_pool_old.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef CPP_TOOLBOX_OLD_THREAD_POOL
4
5# include <vector> // 用于存储工作线程/For storing worker threads
6// #include <queue> // 不再需要标准队列/No longer need standard
7// queue
8# include <thread> // C++ 线程库/C++ thread library
9// #include <mutex> // 不再需要互斥锁/No longer need mutex
10// #include <condition_variable> // 不再需要条件变量/No longer need condition
11// variable
12# include <atomic> // 用于原子布尔标志/For atomic boolean flags
13# include <functional> // 用于 std::function, std::bind/For std::function, std::bind
14# include <future> // 用于异步获取任务结果/For async task results
15# include <iostream> // 用于日志输出/For logging output
16# include <memory> // 用于智能指针/For smart pointers
17# include <stdexcept> // 用于异常处理/For exception handling
18# include <type_traits> // 用于类型特征/For type traits
19# include <utility> // 用于移动语义/For move semantics
20
21# include <cpp-toolbox/cpp-toolbox_export.hpp>
22# include <cpp-toolbox/macro.hpp>
23
25
26namespace toolbox::base
27{
28
60class CPP_TOOLBOX_EXPORT thread_pool_t
61{
62public:
70 explicit thread_pool_t(size_t threads = 0);
71
76 ~thread_pool_t();
77
105 template<class F, class... Args>
106 auto submit(F&& f, Args&&... args)
107 -> std::future<typename std::invoke_result_t<F, Args...>>;
108
109 CPP_TOOLBOX_DISABLE_COPY(thread_pool_t)
110 CPP_TOOLBOX_DISABLE_MOVE(thread_pool_t)
111
112private:
113 std::vector<std::thread> workers_; // 工作线程列表/List of worker threads
114 toolbox::container::lock_free_queue_t<std::function<void()>>
115 tasks_; // 无锁任务队列/Lock-free task queue
116 std::atomic<bool> stop_; // 停止标志/Stop flag
117};
118
119template<class F, class... Args>
120auto thread_pool_t::submit(F&& f, Args&&... args)
121 -> std::future<typename std::invoke_result_t<F, Args...>>
122{
123 using return_type = typename std::invoke_result_t<F, Args...>;
124
125 if (stop_.load(
126 std::memory_order_relaxed)) { // 使用 relaxed 因为后续 enqueue
127 // 有自己的同步/Use relaxed since
128 // enqueue has its own synchronization
129 throw std::runtime_error(
130 "在已停止的线程池上提交任务/Submitting task to stopped thread pool");
131 }
132
133 auto task = std::make_shared<std::packaged_task<return_type()>>(
134 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
135
136 std::future<return_type> res = task->get_future();
137
138 tasks_.enqueue(
139 [task /* Capture shared_ptr */]()
140 {
141 try {
142 (*task)();
143 } catch (const std::future_error& fe) {
144 std::cerr << "Worker lambda caught future_error for task "
145 << task.get() << ": " << fe.what() << std::endl;
146 } catch (const std::exception& e) {
147 std::cerr << "Worker lambda caught unexpected exception during "
148 "packaged_task invocation for task "
149 << task.get() << ": " << e.what() << std::endl;
150 } catch (...) {
151 std::cerr << "Worker lambda caught unknown exception during "
152 "packaged_task invocation for task "
153 << task.get() << std::endl;
154 }
155 });
156
157 return res;
158}
159
160} // namespace toolbox::base
161
162#endif // CPP_TOOLBOX_OLD_THREAD_POOL
#define CPP_TOOLBOX_DISABLE_COPY(ClassType)
禁用类的拷贝操作 / Disable copy operations for a class
Definition class.hpp:15
#define CPP_TOOLBOX_DISABLE_MOVE(ClassType)
禁用类的移动操作 / Disable move operations for a class
Definition class.hpp:31
auto submit(F &&f, Args &&... args) -> std::future< typename std::invoke_result_t< F, Args... > >
Definition thread_pool.hpp:166
Definition lock_free_queue.hpp:337
通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macro...
提供基础 C++ 工具和组件。 Provides fundamental C++ utilities and components.