loulett

mutex

Feb 25th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <thread>
  2. #include <vector>
  3. #include <iostream>
  4. #include <mutex>
  5.  
  6. std::mutex mutex;
  7.  
  8. void hello(int x) {
  9.     mutex.lock();
  10.     std::cout << "Hello, Concurrent World!" << x << '\n';
  11.     mutex.unlock();
  12.     return;
  13. }
  14.  
  15. int main() {
  16.     int x;
  17.     std::vector<std::thread> arr;
  18.     std::cin >> x;
  19.     for (int i = 0; i < x; i++) {
  20.         arr.push_back(std::thread(hello, i));
  21.     }
  22.     for (int i = 0; i < x; i++) {
  23.         arr[i].join();
  24.     }
  25.     return 0;
  26.  }
Advertisement
Add Comment
Please, Sign In to add comment