Reference: std::async
Inside main() within the stated example:
int main()
{
std::vector<int> v(10000, 1);
std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
X x;
// Calls (&x)->foo(42, "Hello") with default policy:
// may print "Hello 42" concurrently or defer execution
auto a1 = std::async(&X::foo, &x, 42, "Hello");
// Calls x.bar("world!") with deferred policy
// prints "world!" when a2.get() or a2.wait() is called
auto a2 = std::async(std::launch::deferred, &X::bar, x, "world!");
// Calls X()(43); with async policy
// prints "43" concurrently
auto a3 = std::async(std::launch::async, X(), 43);
a2.wait(); // prints "world!"
std::cout << a3.get() << '\n'; // prints "53"
} // if a1 is not done at this point, destructor of a1 prints "Hello 42" here
My understanding is that all the invocations using std::async
above should confirm to the following bind semantics:
Reference:
std::bind
As described in Callable, when invoking a pointer to non-static member function or pointer to non-static data member, the first argument has to be a reference or pointer (including, possibly, smart > pointer such as std::shared_ptr and std::unique_ptr) to an object whose member will be accessed.
The first invocation does meet the above requirement; the second does too, albeit a reference to the object is passed instead of a pointer; but what about the third? The third invocation does not bind to an object. Is this usage correct?
According to operator overloading
Function call operator
When a user-defined class overloads the function call operator operator(), it becomes a FunctionObject type.
So I would assume that in the third case, the binding is with the function object thus created.