fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <future>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. int bar() { return 42; }
  10.  
  11. int main()
  12. {
  13. auto s2 = std::chrono::steady_clock::now();
  14. auto sh_ptr1 = std::make_shared<std::packaged_task<int()> >(bar);
  15. auto e2 = std::chrono::steady_clock::now();
  16.  
  17. auto s1 = std::chrono::steady_clock::now();
  18. std::packaged_task<int()> pt1(bar);
  19. auto e1 = std::chrono::steady_clock::now();
  20.  
  21. auto first = std::chrono::duration_cast<std::chrono::nanoseconds>(e1-s1);
  22. auto second = std::chrono::duration_cast<std::chrono::nanoseconds>(e2-s2);
  23.  
  24. cout << "Regular: " << first.count() << endl
  25. << "Make shared: " << second.count() << endl;
  26.  
  27. pt1();
  28. (*sh_ptr1)();
  29.  
  30. cout << "As you can see, both are working correctly: "
  31. << pt1.get_future().get() << " & "
  32. << sh_ptr1->get_future().get() << endl;
  33.  
  34. return 0;
  35. }
Runtime error #stdin #stdout #stderr 0s 3240KB
stdin
Standard input is empty
stdout
Regular: 548
Make shared: 68065
stderr
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1