Home Ask Login Register

Developers Planet

Your answer is one click away!

Michael February 2016

Is calling std::copy from multiple threads for different ranges of the same vector safe?

I am computing floats from multiple threads and storing the results in non-overlapping ranges of the the same vector<float> as follows:

Before running any of the threads I pre-allocated it using vector::reserve.

In each thread a thread-specific vector of results is computed and then copied into the target container like this:

vector<float>::iterator destination = globalVector.begin() + threadSpecificIndex;
std::copy( localVector.begin(), localVector.end(), destination );

Is this a safe practice?


Piotr SmaroĊ„ February 2016

If the vector has fixed size (and it seems like it has from your question), and ranges are not overlapping, then:

  1. vector won't be reallocated
  2. different threads won't access the same memory

thus I don't see any data races here. (But according to 1st comment to your question, you must ensure, that vector has this fixed size when it is used). You can see also "Data Races" section for std::copy: http://www.cplusplus.com/reference/algorithm/copy/

NathanOliver February 2016

First vector::reserve doesn't actually create any elements. It just sets the the capacity of the vector. If you need the elements to be there you need vector::resize or just construct the vector to the size needed.

Secondly the rule of thumb is if you have a shared object between threads and at least one of them is a writer you need synchronization. Since in this case the "object" is the iterator range and they do not overlap you are okay in this regard. As long as the vectors size is not changed then you should be okay.

One issue you could have with this is false sharing. If the same cache line contains variables that different threads are using then those cache lines have to re-synchronized every time a variable in the line is updated. This can slow down the performance of the code quite a bit.

Post Status

Asked in February 2016
Viewed 1,994 times
Voted 8
Answered 2 times


Leave an answer

Quote of the day: live life