Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3. #include <thread>
  4. bool stopAtTheCrossroad()
  5. {
  6.     std::cout << std::this_thread::get_id();
  7.     char ch;
  8.     std::cin.get(ch);
  9.     if (ch == 'q')
  10.         return true;
  11.    
  12.     return false;
  13. }
  14.  
  15. int main()
  16. {
  17.     bool exit{false};
  18.     std::thread th([&exit]() { exit = stopAtTheCrossroad(); });
  19.     for (int i = 0; true; ++i)
  20.     {
  21.         std::cout << "iter " << i + 1 << ": \tprint, thread id = " << std::this_thread::get_id() << std::endl;
  22.         std::this_thread::sleep_for(std::chrono::milliseconds(300));
  23.         if (exit)
  24.             break;
  25.     }
  26.     th.join();
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement