fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <utility>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. class OnExitImpl : private T
  9. {
  10. public:
  11. template <typename Y>
  12. OnExitImpl(Y&& todo) : T(std::forward<Y>(todo)), _doIt(true) {}
  13.  
  14. OnExitImpl(OnExitImpl&& other) : T(std::move(static_cast<T&>(other))), _doIt(other._doIt)
  15. {
  16. other._doIt = false;
  17. }
  18.  
  19. ~OnExitImpl()
  20. {
  21. if (_doIt)
  22. {
  23. (*this)();
  24. }
  25. }
  26.  
  27. void Cancel()
  28. {
  29. _doIt = false;
  30. }
  31.  
  32. OnExitImpl& operator=(OnExitImpl&& other)
  33. {
  34. this->T::operator=(std::move(static_cast<T&>(other)));
  35. _doIt = other._doIt;
  36. other._doIt = false;
  37. }
  38.  
  39. private:
  40. bool _doIt;
  41. };
  42.  
  43. template <typename T>
  44. OnExitImpl<T> OnExit(T action)
  45. {
  46. return OnExitImpl<T>(std::move(action));
  47. }
  48.  
  49. int FetchMultithreaded(int stmt)
  50. {
  51. std::thread* threadPtr = nullptr;
  52. auto onExit = OnExit([&](){ cout << stmt << endl; });
  53.  
  54. std::thread fetchThread([&]()
  55. {
  56. auto onExit = OnExit([&](){ cout << stmt << endl; });
  57. });
  58.  
  59. return 0;
  60. }
  61.  
  62. int main()
  63. {
  64. return FetchMultithreaded(0);
  65. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/home/wKP0IY/ccZGAB13.o: In function `FetchMultithreaded(int)':
prog.cpp:(.text+0x5c2): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty