如何异步值映射到与线程功能? [英] How do I asynchronously map values onto a function with threading?

查看:170
本文介绍了如何异步值映射到与线程功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何执行尴尬,在C ++ 11的并行任务。一个常见的​​模式我遇到是获得一个函数的结果时,在一定范围内的值进行评估,类似于调用python的<一个href=\"http://docs.python.org/dev/library/multiprocessing.html#multiprocessing.pool.multiprocessing.Pool\"相对=nofollow> multiprocessing.Pool.map 。我写了一个小例子,显示什么,我知道该怎么做,即调用一个过程,并等待结果。我怎样才能地图这一呼吁异步和等待,直到所有值都做了什么?理想情况下,我想在相同的长度和秩序作为原始的向量的结果。

 的#include&LT;&iostream的GT;
#包括LT&;螺纹&GT;
#包括LT&;&未来GT;
#包括LT&;矢量&GT;使用命名空间std;双square_add(双X,双Y){返回X * X + Y; }诠释主(){
  矢量&lt;&双GT; A = {1,2,3,4,5};  //单评测
  汽车single_result =的std ::异步(square_add,A [2],3);
  COUT&LT;&LT; 评估一个索引&LT;&LT; single_result.get()&所述;&下; ENDL;  //阻塞地图
  为(自动&安培; X:A){
    汽车blocking_result =的std ::异步(square_add,X,3);
    COUT&LT;&LT; 评估一个索引&LT;&LT; blocking_result.get()&所述;&下; ENDL;
  }  //非阻塞地图?  返回0;
}

请注意:得到这个code编译与 GCC 我需要的 -pthreads 标记<。 / p>

解决方案

的std ::异步返回以后,这样你就可以将它们存储在供以后消费向量:

 的std ::矢量&lt;的std ::未来&LT;双&GT;&GT; future_doubles;
future_doubles.reserve(A.size());
为(自动&安培; X:A){
    //可能会阻止,但也可能不会。
    future_doubles.push_back(性病::异步(square_add,X,3));
}//现在在一个时间上所有的人一挡。
为(自动&安培; f_d:future_doubles){
    性病::法院LT&;&LT; f_d.get()&所述;&下;的std :: ENDL;
}

现在上面的code可能会或可能不会异步运行。这取决于执行/系统来决定是否它是值得的异步或不执行该任务。如果你想迫使它在单独的线程运行,您可以通过一个可选的launch_policy到std ::异步,改变调用

  future_doubles.push_back(性病::异步(的std ::推出::异步,square_add,X,3));

有关的std ::异步和各项政策的详细信息,请参阅的这里

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 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.

这篇关于如何异步值映射到与线程功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆