2014-01-07 2 views
1

나는 vC++ 2013 익스프레스 에디션을 사용하고 있습니다. std :: async와 std :: future 같은 C++ 11의 새로운 기능을 연구 중입니다.표준 가속화 : 새로운 C++ 11 std :: async 채우기 .vector

나는 Foo 클래스가 있으며 std::shared_ptr<std::vector<unsigned int> >입니다. Foo ctor에서 std :: make_shared를 사용하여 힙에 벡터를 할당합니다. foo.cpp의에서 Foo.h

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

} 

에서

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

} 

목 문제는 내가 표준 : 비동기 호출 (표준 : : 출시 :: 비동기 사이에 어떤 이득을 볼 수있다 , & Foo :: fooFunction, this) .get(); 및 fooFunction();

왜 ?? 도움이 될 것입니다. 감사합니다.

+2

시간이 다른 이유는 무엇입니까? 어쨌든 코드가 효과적으로 직렬화됩니다. – zch

답변

2

std::asyncstd::future을 반환합니다.

나중에 get()을 호출하면 결과를 사용할 수있을 때까지 대기 한 다음 반환합니다. 따라서 비동기 적으로 실행 되더라도 아무 것도하지 않고 결과를 기다리고있는 것입니다.

std::asyncFoo::fooFunctionfor 루프를 마술처럼 병렬화하지 않습니다.