Advertisement
vladislav_larionov

Untitled

Feb 5th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 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. std::mutex mutex;
  9.  
  10. void threadFunction()
  11. {
  12. //std::unique_lock<std::mutex> lock(mutex);
  13. a.push_back(1);
  14. for (size_t i = 0; i < 10; i++)
  15. {
  16. a.push_back(i);
  17. // a.insert(a.begin(), i);
  18. std::cout << std::this_thread::get_id() << ": " << a.size() << std::endl;
  19. sleep(1);
  20. }
  21.  
  22. }
  23.  
  24. int main()
  25. {
  26. std::thread thr1(threadFunction);
  27. std::thread thr2(threadFunction);
  28. std::cout << "_thr1 id = " << thr1.get_id() << "_" << std::endl;
  29. std::cout << "_thr2 id = " << thr2.get_id() << "_" << std::endl;
  30. thr1.join();
  31. thr2.join();
  32. std::cout << a.size() << std::endl;
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement