Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. int quickComputation(); // process the result "quick and dirty"
  2. int accurateComputation(); // process the result "accurate but slow"
  3.  
  4. // outside declared because lifetime of accurateComputation() might exceed
  5. // lifetime of bestResultInTime()
  6. std::future<int> f;
  7.  
  8. int bestResultInTime()
  9. {
  10. // define the time slot to get the answer
  11. auto tp = std::chrono::system_clock::now() + std::chrono::minutes(1);
  12.  
  13. // start both a quick and an accurate computation
  14. f = std::async(std::launch::async, accurateComputation);
  15. int guess = quickComputation();
  16.  
  17. // give accurate computation the rest of the time slot
  18. std::future_status s = f.wait_until(tp);
  19.  
  20. // return the best computation result we have
  21. if (s == std::future_status::ready)
  22. return f.get();
  23. else
  24. return guess; // accurateComputation() CONTINUES!!!!!
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement