Class y std :: async on class member en C ++
Intento escribir un miembro de la clase que llama a otro miembro de la clase varias veces en paralelo.
Escribí un ejemplo simple del problema y ni siquiera puedo compilar esto. ¿Qué hago mal al llamar std :: async? Supongo que el problema sería con cómo estoy pasando la función.
#include <vector>
#include <future>
using namespace std;
class A
{
int a,b;
public:
A(int i=1, int j=2){ a=i; b=j;}
std::pair<int,int> do_rand_stf(int x,int y)
{
std::pair<int,int> ret(x+a,y+b);
return ret;
}
void run()
{
std::vector<std::future<std::pair<int,int>>> ran;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
auto hand=async(launch::async,do_rand_stf,i,j);
ran.push_back(hand);
}
}
for(int i=0;i<ran.size();i++)
{
pair<int,int> ttt=ran[i].get();
cout << ttt.first << ttt.second << endl;
}
}
};
int main()
{
A a;
a.run();
}
Compilacion:
g++ -std=c++11 -pthread main.cpp