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