Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mutex>
- #include <thread>
- #include <unistd.h>
- #include <vector>
- std::vector<int> a;
- std::mutex mutex;
- void threadFunction()
- {
- //std::unique_lock<std::mutex> lock(mutex);
- a.push_back(1);
- for (size_t i = 0; i < 10; i++)
- {
- a.push_back(i);
- // a.insert(a.begin(), i);
- std::cout << std::this_thread::get_id() << ": " << a.size() << std::endl;
- sleep(1);
- }
- }
- int main()
- {
- std::thread thr1(threadFunction);
- std::thread thr2(threadFunction);
- std::cout << "_thr1 id = " << thr1.get_id() << "_" << std::endl;
- std::cout << "_thr2 id = " << thr2.get_id() << "_" << std::endl;
- thr1.join();
- thr2.join();
- std::cout << a.size() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement