Thread-safety of writing a std::vector vs plain array

前端 未结 4 2045
离开以前
离开以前 2021-02-07 03:47

I\'ve read on Stackoverflow that none of the STL containers are thread-safe for writing. But what does that mean in practice? Does it mean I should store writab

4条回答
  •  隐瞒了意图╮
    2021-02-07 04:36

    In this case you should just construct your vector with necessary number of values? and all will work fine.

    std::vector data(n, 0);
    

    resize() works well to. The performance will be equal. The reason why multithread access will not corrupt the vector is: your data is located at its place and will not move from there. OMP threads will not access to the same element at a time.

提交回复
热议问题