简体   繁体   中英

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

I am computing float s 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?

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM