Let\'s say I have a vector of N elements, but up to n elements of this vector have meaningful data. One updater thread updates the nth or n+1st element (then sets n = n+1),
Is how your workers decide to work on data thread safe? Is there any signaling between workers done and the producer? If not then there is definitely an issue where the producer could cause the vector to move while it is still being worked on. Though this could trivially be fixed by moving to a std::deque
instead.(note that std::deque
invalidates iterators on push_back
but references to elements are not affected).
I want to ask whether a problem may occur during reallocating of vector to a larger memory block, if there are some child working threads left from the previous update.
Yes, this would be very bad.
If you are using a container from multiple threads and at least one thread may perform some action that may modify the state of the container, access to the container must be synchronized.
In the case of std::vector
, anything that changes its size (notably, insertions and erasures) change its state, even if a reallocation is not required (any insertion or erasure requires std::vector
's internal size bookkeeping data to be updated).
One solution to your problem would be to have the producer dynamically allocate the std::vector
and use a std::shared_ptr<std::vector<T> >
to own it and give this std::shared_ptr
to each of the consumers.
When the producer needs to add more data, it can dynamically allocate a new std::vector
with a new, larger size and copies of the elements from the old std::vector
. Then, when you spin off new consumers or update consumers with the new data, you simply need to give them a std::shared_ptr
to the new std::vector
.
I've made my own GrowVector. It works for me and it is really fast.
Link: QList, QVector or std::vector multi-threaded usage