문제


it is known that operator [] is not concurrently safe for writing: concurrent_vector::operator[] Operator But what if I guarantee that different threads will write to different vector positions. Like this (very much simplified example):

concurrent_vector<double> vec;
vec.resize(100);

parallel_for(0, 100, [&] (double ind)
{
    vec[ind] = ind*ind;
}

Is it concurrently safe or not? And if 'not' then why?

Thanks

도움이 되었습니까?

해결책

Yes, it is concurrently safe as it's like if you are accessing different variables.

As ildjam pointed out in the comment that would be safe even with regular std::vector or, let me add, with simple arrays.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top