fork(2) download
  1. #include <iostream>
  2. #include <future>
  3. #include <functional>
  4. #include <type_traits>
  5.  
  6. template<class Fn, class... Args>
  7. inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
  8. auto make_call = std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...);
  9.  
  10. return std::async(std::launch::async, [=]() -> decltype(make_call()) {
  11. try {
  12. return make_call();
  13. } catch (...) {
  14. std::cout << "Terminate Called!" << std::endl;
  15. std::terminate();
  16. }
  17. });
  18. }
  19.  
  20. struct Foo {
  21. void print() {
  22. printf("Foo::print()\n");
  23. }
  24. void print2() {
  25. printf("Foo::print2()\n");
  26. throw 1;
  27. }
  28. };
  29.  
  30. int main() {
  31. Foo foo;
  32. std::future<void> future = runAsyncTerminateOnException(&Foo::print, &foo);
  33. std::future<void> future2 = runAsyncTerminateOnException(&Foo::print2, &foo);
  34. return 0;
  35. }
Runtime error #stdin #stdout #stderr 0s 8600KB
stdin
Standard input is empty
stdout
Foo::print2()
Terminate Called!
stderr
terminate called after throwing an instance of 'int'