Advertisement
vladislav_larionov

Untitled

Feb 5th, 2025 (edited)
10
0
12 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <mutex>
  3. #include <thread>
  4. #include <unistd.h>
  5. #include <vector>
  6.  
  7. std::vector<int> a;
  8. int b = 1;
  9. std::mutex mutex;
  10.  
  11. void threadFunctionInt()
  12. {
  13. // std::unique_lock<std::mutex> lock(mutex);
  14. for (size_t i = 0; i < 10; i++)
  15. {
  16. std::unique_lock<std::mutex> lock(mutex);
  17. b++;
  18. std::cout << std::this_thread::get_id() << ": " << b << std::endl;
  19. sleep(1);
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. // std::cout << "at top" << std::endl;
  26. std::cout << "at for" << std::endl;
  27. std::thread thr1(threadFunctionInt);
  28. std::thread thr2(threadFunctionInt);
  29. std::cout << "_thr1 id = " << thr1.get_id() << "_" << std::endl;
  30. std::cout << "_thr2 id = " << thr2.get_id() << "_" << std::endl;
  31. thr1.join();
  32. thr2.join();
  33. std::cout << a.size() << std::endl;
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement