Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. BOOST_AUTO_TEST_CASE( exception_in_thread )
  2. {
  3. fc::thread thread("my");
  4. std::atomic<int> counter;
  5.  
  6. counter = 0;
  7.  
  8. class my_exception : public std::exception
  9. {
  10. public:
  11. my_exception(int t) : thread_id(t) {}
  12. const char* what() const throw ()
  13. {
  14. return (std::string("Error in thread ") + std::to_string(thread_id)).c_str();
  15. }
  16. int thread_id;
  17. };
  18.  
  19. try {
  20. auto future1 = thread.async( []() { ilog("In async1"); throw my_exception(1); });
  21. auto future2 = thread.async( []() { ilog("In async2"); throw my_exception(2); });
  22. auto future3 = thread.async( []() { ilog("In async3"); throw my_exception(3); });
  23. future1.wait();
  24. future2.wait();
  25. future3.wait();
  26. }
  27. catch (const my_exception& e)
  28. {
  29. counter++;
  30. elog("Exception caught on another fc::thread: ${msg}", ("msg", e.what() ));
  31. }
  32.  
  33. // is thread still alive?
  34. auto future = thread.async([]() { return; });
  35. future.wait();
  36. ilog("Counter was called ${cnt} times.", ("cnt", counter) );
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement