fork download
  1. #include <future>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. int main() {
  6. std::vector<std::future<int>> futures;
  7.  
  8. for(int i = 0; i < 4; ++i) {
  9. futures.emplace_back (std::async(
  10. std::launch::async,[](int a,int b){return a*b;}, i,i));
  11. }
  12.  
  13. double r;
  14. for(auto &e : futures) {
  15. auto temp = e.get();
  16. std::cout << temp << std::endl;
  17. r +=temp;
  18. }
  19. std::cout<<r<<std::endl;
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 36200KB
stdin
Standard input is empty
stdout
0
1
4
9
14