Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class and std::async on class member in C++

Tags:

c++

c++11

I'm try to write a class member which calls another class member multiple times in parallel.

I wrote a simple example of the problem and can't even get to compile this. What am I doing wrong with calling std::async? I guess the problem would be with how I'm passing the the function.

#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(); } 

compilation:

g++ -std=c++11 -pthread main.cpp  
like image 940
UldisK Avatar asked Aug 01 '12 11:08

UldisK


People also ask

What is std :: async?

The function template async runs the function f asynchronously (potentially in a separate thread which might be a part of a thread pool) and returns a std::future that will eventually hold the result of that function call. 1) Behaves as if (2) is called with policy being std::launch::async | std::launch::deferred.

Is class member and member function same?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

Can classes have member functions?

In addition to holding data, classes (and structs) can also contain functions! Functions defined inside of a class are called member functions (or sometimes methods). Member functions can be defined inside or outside of the class definition.

How do you call a class member in C++?

Accessing data members and member functions: The data members and member functions of class can be accessed using the dot('. ') operator with the object. For example if the name of object is obj and you want to access the member function with the name printName() then you will have to write obj.


2 Answers

do_rand_stf is a non-static member function and thus cannot be called without a class instance (the implicit this parameter.) Luckily, std::async handles its parameters like std::bind, and bind in turn can use std::mem_fn to turn a member function pointer into a functor that takes an explicit this parameter, so all you need to do is to pass this to the std::async invocation and use valid member function pointer syntax when passing the do_rand_stf:

auto hand=async(launch::async,&A::do_rand_stf,this,i,j); 

There are other problems in the code, though. First off, you use std::cout and std::endl without #includeing <iostream>. More seriously, std::future is not copyable, only movable, so you cannot push_back the named object hand without using std::move. Alternatively, just pass the async result to push_back directly:

ran.push_back(async(launch::async,&A::do_rand_stf,this,i,j)); 
like image 71
JohannesD Avatar answered Oct 04 '22 18:10

JohannesD


You can pass the this pointer to a new thread:

async([this]() {     Function(this); }); 
like image 29
Andrew Avatar answered Oct 04 '22 16:10

Andrew