fork download
  1. #include <thread>
  2. #include <future>
  3. #include <chrono>
  4. #include <iostream>
  5. #include <chrono>
  6. using namespace std;
  7.  
  8. struct SharedStateWhichBlocks {
  9. ~SharedStateWhichBlocks()
  10. {
  11. thread t([]() {
  12. this_thread::sleep_for(std::chrono::seconds(5));
  13. });
  14. t.join();
  15. cout << "~SharedStateWhichBlocks" << endl;
  16. }
  17. };
  18.  
  19. int main()
  20. {
  21.  
  22. auto start = std::chrono::high_resolution_clock::now();
  23. SharedStateWhichBlocks *state = new SharedStateWhichBlocks();
  24. {
  25. promise<SharedStateWhichBlocks> pr;
  26. future<SharedStateWhichBlocks> fut = pr.get_future();
  27. pr.set_value(*state);
  28. cout << "Will now block since ~SharedStateWhichBlocks will be called" << endl;
  29. }
  30. auto end = std::chrono::high_resolution_clock::now();
  31. auto difference = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
  32. std::cout << "Seconds for the block { ... } " << difference << endl;
  33. }
Success #stdin #stdout 0s 17296KB
stdin
Standard input is empty
stdout
Will now block since ~SharedStateWhichBlocks will be called
~SharedStateWhichBlocks
Seconds for the block { ... } 5