Write the <bool> vector at the same time

I know that you can read at the same time with std::vectorno "bad" consequences, because this operation can be considered thread safe.

But the same cannot be said for written operations. But I wonder if this is not so, for example, given my specific scenario.

I have std::vector<bool>where all the elements are initialized to false, and given the array of indexes, I need to change the value of these elements ( vector[index]for each index) from falseto true.

If I use a different thread for each index (and it is likely that some indexes have the same value), can this operation be considered thread safe?

If the vector is std::vector<int>(or any primitive type) and the assigned value is always the same (e.g. 1), can this operation still be considered thread safe?

+5
source share
2 answers

Parallel writing is vector<bool>never performed, because the base implementation is based on a proxy type object vector<bool>::referencethat acts as if it were a reference to bool, but will actually extract and update bytes of the bit field as needed.

: 1 . 2 , 1 , 2 , 1.

, , .


vector<int>, , , . - , .

+10

[container.requirements.dataraces]/2 :

(17.6.5.9), , vector<bool>, .

, , , vector<bool>.

+7

All Articles