在async中传递时如何在函数中传递多个参数

时间:2015-10-07 00:26:36

标签: c++ asynchronous parameter-passing

我想为一个函数传递两个参数,当在异步函数中传递该函数时,该函数将两个参数作为参数。我从来没有使用异步,所以我不知道该怎么做

所以这是函数

double NearestPoints::otherCoordinate(Coordinate coordinate1, Coordinate** secondCoordinate){

这是异步功能

std::future<double> ret = std::async(&otherCoordinate,coordinate1,ref(coordinate2));

我很确定我是以错误的方式实现该功能,但我只是想知道正确的实现。

提前致谢!

3 个答案:

答案 0 :(得分:1)

这是你在找什么?

#include <iostream>    
#include <future>      


int add(int x,int y) {    
    return x+y;
}

int main()
{  
    std::future<int> fut = std::async(add, 10,20);
    int ret = fut.get();
    std::cout << ret << std::endl;       
    return 0;
}

答案 1 :(得分:0)

从我的问题中我可以看出,您似乎忘记将NearestPoints个实例传递给std::async来电。由于NearestPoints::otherCoordinate是一个成员函数,因此需要为其NearestPoints指针传递this类的实例。

要解决此问题,您应该传入当前实例的副本,以便该函数可以访问要操作的实例。

您对std::async的固定电话看起来像这样:

std::future<double> ret = std::async(&NearestPoints::otherCoordinate, *this, coordinate1, std::ref(coordinate2));

答案 2 :(得分:0)

按以下方式修复您的电话:

KeyListener