Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 318
Michael Avatar asked Feb 08 '16 19:02

Michael


People also ask

Is std :: copy thread-safe?

Yes. If the object is accessed (written to) from other threads and you want to copy it, you have to ensure the access is synchronized. Show activity on this post. Thread-safety is only relevant in the context of shared objects.

Are c++ vector thread-safe?

The C++11 standard does not expect to be able to safely call non const functions simultaneously. Therefore all classes available from the standard, e.g. std::vector<>, can safely be accessed from multiple threads in the same manner.

Can multiple threads call the same function?

There is nothing wrong in calling same function from different threads. If you want to ensure that your variables are consistent it is advisable to provide thread synchronization mechanisms to prevent crashes, racearound conditions.

Is std::vector size thread-safe?

Yes, you are right. I obviously misunderstood that sentence in the documentation: "No contained elements are accessed: concurrently accessing or modifying them is safe." It probably only means that size() is thread-safe against a concurrent modification of the elements already in the container.


2 Answers

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.

like image 177
NathanOliver Avatar answered Nov 15 '22 17:11

NathanOliver


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/

like image 23
Piotr Smaroń Avatar answered Nov 15 '22 18:11

Piotr Smaroń