this is a beginner question i guess but i couldn’t find the answer to this particular question:
i have a standard (c++) vector v of size 10 and type int.
is it safe to have a thread alter all the even positions (v.at(0) = x; v.at(2) = y; etc.)
and another thread alter all the values for the odd positions (v.at(1) = a; v.at(3)=b;etc.) at the same time?
so, no changing of size, no push_back() etc. during the lifetime of these 2 threads.
if it is not safe, would the use of an array be a better way to do this?
thanks for your help.
vectordoes not provide any thread-safety guarantees, so technically the answer would be no.In practice, you should be able to get away with it… until the day that someone (possibly you) makes a small change in one corner of the program and all hell breaks loose. I wouldn’t feel comfortable doing this in any non-trivial program.