fork download
  1. #include <iostream>
  2. #include <thread>
  3. using namespace std;
  4.  
  5. struct Join {};
  6. struct Detach {};
  7.  
  8. template <typename Action>
  9. class Thread {
  10. public:
  11.  
  12. Thread(thread && t) : th(move(t)) {}
  13.  
  14. ~Thread()
  15. {
  16. if (th.joinable()) action(Action());
  17. }
  18.  
  19. private:
  20.  
  21. thread th;
  22.  
  23. void action(Join)
  24. {
  25. th.join();
  26. }
  27.  
  28. void action(Detach)
  29. {
  30. th.detach();
  31. }
  32. };
  33.  
  34. void work()
  35. {
  36. }
  37.  
  38. int main()
  39. {
  40. Thread<Join> t1(thread(work));
  41. Thread<Detach> t2(thread(work));
  42. }
  43.  
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty