This is not related to moving.
Multiple threads execute vector::push_back() on the same vector , but vector::push_back() not thread safe. Changes to vector must be synchronized.
A std::mutex can be used to synchronize calls with push_back() :
std::vector<int> values; std::mutex values_mutex; void values_push_back() { values_mutex.lock(); values.push_back(i); values_mutex.unlock(); }
In addition, the variable i is divided between threads without synchronization, which will lead to a race condition (a possible result of this duplication int added to vector ). Consider passing an int value as an argument to the stream to avoid this:
std::vector<int> values; std::mutex values_mutex; void values_push_back(int i) { values_mutex.lock(); values.push_back(i); values_mutex.unlock(); } for (int i = 0; i < 10; ++i) { threads.push_back(std::thread(values_push_back, i)); } for (auto& t: threads) t.join();
As bamboon pointed out , prefer std::lock_guard to ensure the lock is released if push_back() throws (which in this case can only be bad_alloc() , but if vector changes to hold more complex objects that the constructors throw, it becomes more important):
void values_push_back(int i) { std::lock_guard<std::mutex> lk(values_mutex); values.push_back(i); }
hmjd
source share