2014-01-07 45 views
1

我使用vC++ 2013快速版。 我正在研究C++ 11的一些新功能,如std :: async和std :: future。加速std:.vector填充新的C++ 11 std :: async

我有一個類Foo,std::shared_ptr<std::vector<unsigned int> >。 在Foo ctor我使用std :: make_shared在堆中分配向量;

從foo.h中

Class Foo{ 
public: 
    Foo(); 
private: 
    std::shared_ptr<std::vector<unsigned int> > testVector; 
    unsigned int MAX_ITERATIONS = 800000000; 
    void fooFunction(); 

} 

從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);  
    } 

} 

釷的問題是我不能看到調用的std ::異步(STD ::推出::異步之間的任何增益,& Foo :: fooFunction,this).get();和fooFunction();

爲什麼? 任何幫助將不勝感激。 致以問候

+2

你爲什麼期望時代會不同?無論如何,你的代碼是有效的序列化。 – zch

回答

2

std::async返回一個std::future

對未來調用get()將使其等待,直到結果可用,然後返回它。所以,即使它是異步運行的,你也無所事事,只能等待結果。

std::async不奇蹟般地並行for循環在Foo::fooFunction

相關問題