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


i computing floats multiple threads , storing results in non-overlapping ranges of the same vector<float> follows:

before running of threads pre-allocated using vector::reserve.

in each thread thread-specific vector of results computed , copied target container this:

vector<float>::iterator destination = globalvector.begin() + threadspecificindex; std::copy( localvector.begin(), localvector.end(), destination ); 

is safe practice?

first vector::reserve doesn't create elements. sets the capacity of vector. if need elements there need vector::resize or construct vector size needed.

secondly rule of thumb if have shared object between threads , @ least 1 of them writer need synchronization. since in case "object" iterator range , not overlap okay in regard. long vectors size not changed should okay.

one issue have false sharing. if same cache line contains variables different threads using cache lines have re-synchronized every time variable in line updated. can slow down performance of code quite bit.


Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -