i'm using vc++ 2013 express edition. I'm studying some new feature of c++11 like std::async, and std::future.
I have a class Foo, with an std::shared_ptr<std::vector<unsigned int> >
.
In Foo ctor i use std::make_shared to allocate in the heap the vector;
From Foo.h
Class Foo{
public:
Foo();
private:
std::shared_ptr<std::vector<unsigned int> > testVector;
unsigned int MAX_ITERATIONS = 800000000;
void fooFunction();
}
From Foo.cpp
Foo::Foo(){
testVector = std::make_shared<std::vector<unsigned int> >();
//fooFunction(); this take about 20 sec
std::async(std::launch::async, &Foo::fooFunction, this).get(); // and this about the same!!
}
void Foo:fooFunction(){
for (unsigned int i = 0; i < MAX_ITERATIONS; i++){
testVector->push_back(i);
}
}
Th problem is i can't see any gain between calling std::async(std::launch::async, &Foo::fooFunction, this).get(); and fooFunction();
Why?? Any help will be appreciated. Best regards
std::async returns a std::future.
Calling
get()
on the future will make it wait until the result is available, then return it. So, even if it is run asynchronously, you are doing nothing but waiting for the result.std::async
doesn't magically parallelize thefor
loop inFoo::fooFunction
.