如何使用线程将值异步映射到函数?(How do I asynchronously map values onto a function with threading?)

编程入门 行业动态 更新时间:2024-10-22 18:50:48
如何使用线程将值异步映射到函数?(How do I asynchronously map values onto a function with threading?)

我正在尝试学习如何在C ++ 11中执行“令人尴尬”的并行任务。 我遇到的一个常见模式是在一系列值上计算函数的结果,类似于调用python的multiprocessing.Pool.map 。 我写了一个最小的例子来说明我知道怎么做,即调用一个进程并等待结果。 如何异步“映射”此调用并等待所有值完成? 理想情况下,我希望结果的矢量长度和顺序与原始相同。

#include <iostream> #include <thread> #include <future> #include <vector> using namespace std; double square_add(double x, double y) { return x*x+y; } int main() { vector<double> A = {1,2,3,4,5}; // Single evaluation auto single_result = std::async(square_add,A[2],3); cout << "Evaluating a single index " << single_result.get() << endl; // Blocking map for(auto &x:A) { auto blocking_result = std::async(square_add,x,3); cout << "Evaluating a single index " << blocking_result.get() << endl; } // Non-blocking map? return 0; }

注意:要使用gcc编译此代码,我需要-pthreads标志。

I'm trying to learn how to execute "embarrassingly" parallel tasks in C++11. A common pattern I come across is to get the result of a function when evaluated over a range of values, similar to calling python's multiprocessing.Pool.map. I've written a minimal example that shows what I know how to do, namely call a single process and wait for the result. How can I "map" this call asynchronously and wait until all values are done? Ideally, I'd like the results in a vector of the same length and order as the original.

#include <iostream> #include <thread> #include <future> #include <vector> using namespace std; double square_add(double x, double y) { return x*x+y; } int main() { vector<double> A = {1,2,3,4,5}; // Single evaluation auto single_result = std::async(square_add,A[2],3); cout << "Evaluating a single index " << single_result.get() << endl; // Blocking map for(auto &x:A) { auto blocking_result = std::async(square_add,x,3); cout << "Evaluating a single index " << blocking_result.get() << endl; } // Non-blocking map? return 0; }

Note: to get this code to compile with gcc I need the -pthreads flag.

最满意答案

std :: async返回一个future,因此您可以将它们存储在一个向量中以供以后使用:

std::vector<std::future<double>> future_doubles; future_doubles.reserve(A.size()); for (auto& x : A) { // Might block, but also might not. future_doubles.push_back(std::async(square_add, x, 3)); } // Now block on all of them one at a time. for (auto& f_d : future_doubles) { std::cout << f_d.get() << std::endl; }

现在上面的代码可能会也可能不会异步运行。 由实现/系统决定是否值得以异步方式执行任务。 如果要强制它在单独的线程中运行,可以将可选的launch_policy传递给std :: async,将调用更改为

future_doubles.push_back(std::async(std::launch::async, square_add, x, 3));

有关std::async和各种策略的更多信息,请参见此处 。

std::async returns a future, so you can store them in a vector for later consumption:

std::vector<std::future<double>> future_doubles; future_doubles.reserve(A.size()); for (auto& x : A) { // Might block, but also might not. future_doubles.push_back(std::async(square_add, x, 3)); } // Now block on all of them one at a time. for (auto& f_d : future_doubles) { std::cout << f_d.get() << std::endl; }

Now the above code might or might not run asynchronously. It's up to the implementation/system to decide whether it's worth it to perform the task asynchronously or not. If you want to force it to run in separate threads, you can pass an optional launch_policy to std::async, changing the call to

future_doubles.push_back(std::async(std::launch::async, square_add, x, 3));

For more info on std::async and the various policies, see here.

更多推荐

本文发布于:2023-07-24 10:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   如何使用   函数   asynchronously   threading

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!