Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int quickComputation(); // process the result "quick and dirty"
- int accurateComputation(); // process the result "accurate but slow"
- // outside declared because lifetime of accurateComputation() might exceed
- // lifetime of bestResultInTime()
- std::future<int> f;
- int bestResultInTime()
- {
- // define the time slot to get the answer
- auto tp = std::chrono::system_clock::now() + std::chrono::minutes(1);
- // start both a quick and an accurate computation
- f = std::async(std::launch::async, accurateComputation);
- int guess = quickComputation();
- // give accurate computation the rest of the time slot
- std::future_status s = f.wait_until(tp);
- // return the best computation result we have
- if (s == std::future_status::ready)
- return f.get();
- else
- return guess; // accurateComputation() CONTINUES!!!!!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement