Advertisement
Fillinlak3

Threads

Sep 22nd, 2020
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <conio.h>
  4. #include <windows.h>
  5.  
  6. // -- Macros --
  7. //#define clear system("cls")
  8.  
  9. // -- Global Variables --
  10. static bool s_Enter = false;
  11.  
  12. // -- Namespaces --
  13. using namespace std;
  14.  
  15. // -- Class & Functions --
  16. class Workers {
  17. public:
  18.     void background_job(int&& n)
  19.     {
  20.         using namespace literals::chrono_literals;
  21.         while (!s_Enter)
  22.         {
  23.             cout << "First Function " << n << endl;
  24.             n++;
  25.             this_thread::sleep_for(50ms);
  26.         }
  27.     }
  28.     void checkEnd()
  29.     {
  30.         while (!s_Enter)
  31.             if (_kbhit()) {
  32.                 s_Enter = true;
  33.                 _getch();
  34.             }
  35.     }
  36. };
  37.  
  38. int main()
  39. {
  40.     int ad = 0;
  41.     // Thread header
  42.     cout << "Threads started the job\n\n";
  43.  
  44.     // Declaring threads
  45.     thread child(&Workers::background_job, Workers(), ad);
  46.     thread child2(&Workers::checkEnd, Workers());
  47.  
  48.     // Childs finishing their jobs
  49.     // ..
  50.  
  51.     child.join();
  52.     child2.join();
  53.  
  54.     // Thread bottom
  55.     cout << "\nThreads finished the job " << ad << endl;
  56.  
  57.     // Exit program
  58.     _getch();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement