C++11 std::vector in concurrent environment
This is unrelated to moving.
Multiple threads are executing vector::push_back()
on the same vector
but
vector::push_back()
is not threadsafe. The modifications to the vector
need to be synchronized.
A std::mutex
could be used to synchronize the calls to push_back()
:
std::vector<int> values;
std::mutex values_mutex;
void values_push_back()
{
values_mutex.lock();
values.push_back(i);
values_mutex.unlock();
}
Also, the variable i
is being shared among threads without synchronization which is will result in a race condition (a possible outcome of this is duplicate int
s added to the vector
). Consider passing the int
value as an argument to the thread 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 commented by bamboon prefer std::lock_guard
to ensure the lock is released if push_back()
throws (which in this case could only be bad_alloc()
but if the vector
changes to hold more complex objects that have throwing constructors it becomes more important):
void values_push_back(int i)
{
std::lock_guard<std::mutex> lk(values_mutex);
values.push_back(i);
}
deepskyblue86
Updated on June 03, 2022Comments
-
deepskyblue86 almost 2 years
I had an issue (segfault) running a multithreaded code in C++11. Here it is the code:
#include <vector> #include <thread> std::vector<int> values; int i; void values_push_back() { values.push_back(i); } int main() { while(true) { std::vector<std::thread> threads; for(i=0; i<10; ++i) { std::thread t(values_push_back); threads.push_back(std::move(t)); } for(i=0; i<10; ++i) threads[i].join(); } return 0; }
And here the backtrace on gdb: http://pastebin.com/5b5TN70c
What's wrong in that?
-
deepskyblue86 about 11 yearsI had a more complex issue I can't reproduce with a simple code. Sorry about that.
-
Stephan Dollberg about 11 yearsYour code is not exception-safe. If push_back throws, you will deadlock, use
std::lock_guard
instead. -
xinthose almost 9 yearsalso consider #include <boost/lockfree/queue.hpp> AND boost::lockfree::queue <int> values {100};