Advertisement
Caio_25

mutexSemaforo

May 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include<thread>
  3. #include<mutex>
  4. using namespace std;
  5.  
  6.  
  7. mutex semaforo;
  8.  
  9. void imprime(int n, char c)
  10. {
  11.     semaforo.lock();
  12.     for(int i = 0; i < n; i++)
  13.     {
  14.         cout << c ;
  15.     }cout << endl;
  16.     semaforo.unlock();
  17. }
  18.  
  19. int main()
  20. {
  21.    thread t1(imprime, 50, 'a');
  22.     thread t2(imprime, 50, 'b');
  23.      thread t3(imprime, 50, 'c');
  24.       thread t4(imprime, 50, 'd');
  25.  
  26.       t1.join();
  27.       t2.join();
  28.       t3.join();
  29.       t4.join();
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement