// Submit a function to be executed asynchronously by the pool template <typename F, typename... Args> autosubmit(F &&f, Args &&...args) -> std::future<decltype(f(args...))> ① { // Create a function with bounded parameter ready to execute std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...); ②// 连接函数和参数定义,特殊函数类型,避免左右值错误
// Encapsulate it into a shared pointer in order to be able to copy construct auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func); ③
// Warp packaged task into void function std::function<void()> warpper_func = [task_ptr]() { (*task_ptr)(); }; ④
// Inits thread pool voidinit() { for (int i = 0; i < m_threads.size(); ++i) { m_threads.at(i) = std::thread(ThreadWorker(this, i)); // 分配工作线程 } }
// Waits until threads finish their current task and shutdowns the pool voidshutdown() { m_shutdown = true; m_conditional_lock.notify_all(); // 通知,唤醒所有工作线程
for (int i = 0; i < m_threads.size(); ++i) { if (m_threads.at(i).joinable()) // 判断线程是否在等待 { m_threads.at(i).join(); // 将线程加入到等待队列 } } }
// Submit a function to be executed asynchronously by the pool template <typename F, typename... Args> autosubmit(F &&f, Args &&...args) -> std::future<decltype(f(args...))> { // Create a function with bounded parameter ready to execute std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误
// Encapsulate it into a shared pointer in order to be able to copy construct auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
// Warp packaged task into void function std::function<void()> warpper_func = [task_ptr]() { (*task_ptr)(); };
// Inits thread pool voidinit() { for (int i = 0; i < m_threads.size(); ++i) { m_threads.at(i) = std::thread(ThreadWorker(this, i)); // 分配工作线程 } }
// Waits until threads finish their current task and shutdowns the pool voidshutdown() { m_shutdown = true; m_conditional_lock.notify_all(); // 通知,唤醒所有工作线程
for (int i = 0; i < m_threads.size(); ++i) { if (m_threads.at(i).joinable()) // 判断线程是否在等待 { m_threads.at(i).join(); // 将线程加入到等待队列 } } }
// Submit a function to be executed asynchronously by the pool template <typename F, typename... Args> autosubmit(F &&f, Args &&...args) -> std::future<decltype(f(args...))> { // Create a function with bounded parameter ready to execute std::function<decltype(f(args...))()> func = std::bind(std::forward<F>(f), std::forward<Args>(args)...); // 连接函数和参数定义,特殊函数类型,避免左右值错误
// Encapsulate it into a shared pointer in order to be able to copy construct auto task_ptr = std::make_shared<std::packaged_task<decltype(f(args...))()>>(func);
// Warp packaged task into void function std::function<void()> warpper_func = [task_ptr]() { (*task_ptr)(); };
// 添加两个数字的简单函数并打印结果 voidmultiply(constint a, constint b) { simulate_hard_computation(); constint res = a * b; std::cout << a << " * " << b << " = " << res << std::endl; }
// 添加并输出结果 voidmultiply_output(int &out, constint a, constint b) { simulate_hard_computation(); out = a * b; std::cout << a << " * " << b << " = " << out << std::endl; }
// 结果返回 intmultiply_return(constint a, constint b) { simulate_hard_computation(); constint res = a * b; std::cout << a << " * " << b << " = " << res << std::endl; return res; }
voidexample() { // 创建3个线程的线程池 ThreadPool pool(3);
// 初始化线程池 pool.init();
// 提交乘法操作,总共30个 for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 10; ++j) { pool.submit(multiply, i, j); }
// 使用ref传递的输出参数提交函数 int output_ref; auto future1 = pool.submit(multiply_output, std::ref(output_ref), 5, 6);
// 等待乘法输出完成 future1.get(); std::cout << "Last operation result is equals to " << output_ref << std::endl;
// 使用return参数提交函数 auto future2 = pool.submit(multiply_return, 5, 3);
// 等待乘法输出完成 int res = future2.get(); std::cout << "Last operation result is equals to " << res << std::endl;