#ifndef PIPELINEPIPELINEH #define PIPELINEPIPELINEH #include <tuple> #include…
- User
#ifndef PIPELINE_PIPELINE_H #define PIPELINE_PIPELINE_H
#include #include #include #include #include #include #include "task_queue.h"
template <size_t I, typename... Ts> using Get = typename std::tuple_element<I, std::tuple<Ts...>>::type;
template struct FunctionTraits;
template struct FunctionTraits : public FunctionTraits<decltype(&Function::operator())> {};
template <typename ClassType, typename ReturnType, typename Argument> struct FunctionTraits<ReturnType (ClassType::*)(Argument) const> { using ResultType = ReturnType; using ArgType = std::decay_t; };
template <typename... Funcs> class FastPipeline { constexpr static size_t FUNCS_NUM = sizeof...(Funcs);
using FirstInputType = typename FunctionTraits<Get<0, Funcs...>>::ArgType; using LastReturnType = typename FunctionTraits<Get<FUNCS_NUM - 1, Funcs...>>::ResultType;public: explicit FastPipeline(Funcs... funcs) : functions(funcs...) { stop = false; StartPipelineHelper<0>(); }
void Stop() { if (!stop) { stop = true; StopPipelineHelper<0>(); for (auto &thread : threads) { if (thread.joinable()) { thread.join(); } } } } template <typename U> void Push(U &&data) { std::get<0>(queues).Push(std::forward<U>(data)); } LastReturnType Pop() { return std::get<FUNCS_NUM>(queues).WaitAndPop(); }private: template <size_t I> void StartPipelineHelper() { if constexpr (I < FUNCS_NUM) { threads.emplace_back([this] { while (!stop) { auto input = std::get(queues).WaitAndPop(); if (stop) { return; } auto &functionI = std::get(functions); std::get<I + 1>(queues).Push(functionI(std::move(input))); } }); StartPipelineHelper<I + 1>(); } }
template <size_t I> void StopPipelineHelper() { if constexpr (I < FUNCS_NUM + 1) { std::get<I>(queues).DestroyQueue(); StopPipelineHelper<I + 1>(); } } using QueuesTuple = std::tuple<TaskQueue<FirstInputType>, TaskQueue<typename FunctionTraits<Funcs>::ResultType>...>; QueuesTuple queues; std::tuple<Funcs...> functions; std::vector<std::thread> threads; std::atomic<bool> stop;};
#endif // PIPELINE_PIPELINE_H 我想扩展一下让每个阶段的函数处理线程数可配,同一个阶段的多个线程函数都从std::get(queues).WaitAndPop()拿输入数据处理,要怎么修改
- Assistant
要扩展
FastPipeline以允许每个阶段具有可配置的处理线程数,可以按照以下步骤进行修改:-
添加每个阶段的线程数配置:允许用户为每个阶段指定线程数。这可以通过在构造函数中添加一个
std::array<std::size_t, FUNCS_NUM>参数来实现,其中每个元素表示对应阶段的线程数。 -
修改线程创建逻辑:在
StartPipelineHelper中,对于每个阶段,根据指定的线程数创建多个线程,每个线程都从相应的输入队列中取出数据进行处理。 -
确保线程安全:假设
TaskQueue已经是线程安全的,多个线程可以安全地从同一个队列中取出数据并将结果推送到下一个队列。
以下是修改后的代码示例:
#ifndef PIPELINE_PIPELINE_H #define PIPELINE_PIPELINE_H #include <tuple> #include <functional> #include <queue> #include <thread> #include <vector> #include <atomic> #include <array> #include <utility> #include "task_queue.h" template <size_t I, typename... Ts> using Get = typename std::tuple_element<I, std::tuple<Ts...>>::type; template <typename> struct FunctionTraits; template <typename Function> struct FunctionTraits : public FunctionTraits<decltype(&Function::operator())> {}; template <typename ClassType, typename ReturnType, typename Argument> struct FunctionTraits<ReturnType (ClassType::*)(Argument) const> { using ResultType = ReturnType; using ArgType = std::decay_t<Argument>; }; template <typename... Funcs> class FastPipeline { constexpr static size_t FUNCS_NUM = sizeof...(Funcs); using FirstInputType = typename FunctionTraits<Get<0, Funcs...>>::ArgType; using LastReturnType = typename FunctionTraits<Get<FUNCS_NUM - 1, Funcs...>>::ResultType; public: // 构造函数现在接受一个数组来指定每个阶段的线程数 explicit FastPipeline( const std::array<std::size_t, FUNCS_NUM>& threads_per_stage, Funcs... funcs) : functions(funcs...), stop(false) { StartPipelineHelper<0>(threads_per_stage); } ~FastPipeline() { Stop(); } void Stop() { if (!stop.exchange(true)) { // 使用 exchange 原子操作确保只停止一次 StopPipelineHelper<0>(); for (auto &thread : threads) { if (thread.joinable()) { thread.join(); } } } } template <typename U> void Push(U &&data) { std::get<0>(queues).Push(std::forward<U>(data)); } LastReturnType Pop() { return std::get<FUNCS_NUM>(queues).WaitAndPop(); } private: template <size_t I> void StartPipelineHelper(const std::array<std::size_t, FUNCS_NUM>& threads_per_stage) { if constexpr (I < FUNCS_NUM) { // 为当前阶段创建指定数量的线程 for (std::size_t t = 0; t < threads_per_stage[I]; ++t) { threads.emplace_back([this, I]() { while (!stop) { typename FunctionTraits<Get<I, Funcs...>>::ArgType input; bool has_item = std::get<I>(queues).TryWaitAndPop(input); if (!has_item) { if (stop) return; continue; } auto &functionI = std::get<I>(functions); auto output = functionI(std::move(input)); std::get<I + 1>(queues).Push(std::move(output)); } }); } // 递归调用下一个阶段 StartPipelineHelper<I + 1>(threads_per_stage); } } template <size_t I> void StopPipelineHelper() { if constexpr (I < FUNCS_NUM + 1) { std::get<I>(queues).DestroyQueue(); StopPipelineHelper<I + 1>(); } } using QueuesTuple = std::tuple<TaskQueue<FirstInputType>, TaskQueue<typename FunctionTraits<Funcs>::ResultType>...>; QueuesTuple queues; std::tuple<Funcs...> functions; std::vector<std::thread> threads; std::atomic<bool> stop; }; #endif // PIPELINE_PIPELINE_H详细说明
-
构造函数修改:
- 构造函数现在接受一个
std::array<std::size_t, FUNCS_NUM>参数threads_per_stage,该数组的每个元素对应每个阶段的线程数。 - 在初始化列表中,保留了
functions(funcs...)和stop(false)的初始化。 - 使用
StartPipelineHelper<0>(threads_per_stage)启动所有阶段的线程。
- 构造函数现在接受一个
-
启动线程的辅助函数
StartPipelineHelper:- 对于每个阶段
I,根据threads_per_stage[I]创建相应数量的线程。 - 每个线程执行一个循环,从输入队列中取出数据,应用相应的函数,并将结果推送到下一个队列中。
- 使用递归模板编程的方法,处理所有阶段。
- 对于每个阶段
-
停止线程的辅助函数
StopPipelineHelper:- 依次销毁每个队列,以确保所有等待的线程能够退出。
- 递归销毁所有队列,确保覆盖所有阶段。
-
线程安全和队列管理:
- 假设
TaskQueue已经是线程安全的,允许多个线程同时从一个队列中取出数据和向一个队列中推送数据。 - 在
Stop函数中,使用std::atomic<bool>的exchange操作确保Stop只执行一次,防止多次停止。
- 假设
-
异常安全:
- 增加了析构函数
~FastPipeline,确保在对象销毁时自动调用Stop,防止资源泄漏。
- 增加了析构函数
使用示例
以下是如何使用修改后的
FastPipeline与多个线程的示例:#include "pipeline.h" #include "task_queue.h" #include <iostream> #include <string> #include <array> int main() { // 定义处理函数 auto func1 = [](int x) -> double { return x * 2.5; }; auto func2 = [](double y) -> std::string { return "Result: " + std::to_string(y); }; // 定义每个阶段的线程数 std::array<std::size_t, 2> threads_per_stage = { 2, 3 }; // 第一阶段2个线程,第二阶段3个线程 // 创建管道 FastPipeline<decltype(func1), decltype(func2)> pipeline(threads_per_stage, func1, func2); // 推送数据 for (int i = 0; i < 10; ++i) { pipeline.Push(i); } // 取出处理结果 for (int i = 0; i < 10; ++i) { std::string result = pipeline.Pop(); std::cout << result << std::endl; } // 停止管道 pipeline.Stop(); return 0; }在这个示例中:
- 管道有两个阶段:
- 第一阶段将
int转换为double,使用2个线程。 - 第二阶段将
double转换为std::string,使用3个线程。
- 第一阶段将
- 推送10个整数,随后取出10个结果。
通过这种方式,可以为每个阶段配置不同数量的处理线程,以提高并行处理能力和吞吐量。
-