STL vector and thread-safety

后端 未结 3 1756
攒了一身酷
攒了一身酷 2020-11-28 14:48

Let\'s say I have a vector of N elements, but up to n elements of this vector have meaningful data. One updater thread updates the nth or n+1st element (then sets n = n+1),

3条回答
  •  醉梦人生
    2020-11-28 14:55

    Is how your workers decide to work on data thread safe? Is there any signaling between workers done and the producer? If not then there is definitely an issue where the producer could cause the vector to move while it is still being worked on. Though this could trivially be fixed by moving to a std::deque instead.(note that std::deque invalidates iterators on push_back but references to elements are not affected).

提交回复
热议问题