Momir

Multithreading bez čekanja

Feb 26th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <thread>
  4. using namespace std;
  5.  
  6. int foo()
  7. {
  8.     for (int i = 0; i < 5; i++) {
  9.         cout << "Thread " << i << endl;  
  10.         Sleep(500);
  11.     }
  12.  
  13.     return 0;
  14. }
  15.  
  16. int foo2()
  17. {
  18.     for (int i = 0; i < 5; i++) {
  19.         cout << "Thread2 " << i << endl;  
  20.         Sleep(500);
  21.     }
  22.  
  23.     return 0;
  24. }
  25.                
  26. int main()
  27. {
  28.     cout << "Pocetak" << endl;
  29.     Sleep(500);
  30.  
  31.     thread th1(foo);
  32.     thread th2(foo2);
  33.    
  34.     int i = 0;     
  35.     while (i++ < 10) {
  36.         cout << "Kraj " << i << endl;
  37.         Sleep(500);
  38.     }
  39.  
  40.     th1.detach();
  41.     th2.detach();
  42.  
  43.     return 0;
  44. }
Add Comment
Please, Sign In to add comment