美文网首页
C++ promise&lambda

C++ promise&lambda

作者: rogerxfederer | 来源:发表于2017-01-19 21:00 被阅读0次

promise&lambda 神器

void CPromiseExample::run(){
    shared_ptr<CPromiseExample> spThis =  shared_from_this();
    boost::future<int> fB = asyncTaskA(101).then([&](boost::future<int> f){
        int nValue = f.get();
        return asyncTaskB(nValue + 1).get();
    }).then([spThis](boost::future<int> f){
        spThis;
        int nValue = f.get();
        return nValue;
    });;

    std::cout<<"endl\n";
}

boost::future<int> CPromiseExample::asyncTaskA(int paraA){
    boost::shared_ptr<boost::promise<int>> promiseA(new boost::promise<int>());
    boost::async([promiseA, paraA]() {
        sleep(5);
        std::cout<<"asyncTaskA\n";
        promiseA->set_value(paraA);
        
    });
    
    return promiseA->get_future();
}

boost::future<int> CPromiseExample::asyncTaskB(int paraB){
    boost::shared_ptr<boost::promise<int>> promiseB(new boost::promise<int>());
    boost::async([promiseB, paraB](){
        sleep(5);
       
        std::cout<<"asyncTaskB\n";
        promiseB->set_value(paraB);
    });
    
    return promiseB->get_future();
}

相关文章

网友评论

      本文标题:C++ promise&lambda

      本文链接:https://www.haomeiwen.com/subject/dnaibttx.html