Is the C++ std::set thread-safe?

前端 未结 6 1007
刺人心
刺人心 2020-11-28 12:34

I\'ve a question about the thread safety of std::set.

As far as I know I can iterate over a set and add/erase members and that doesn\'t invalidate the iterators.

6条回答
  •  伪装坚强ぢ
    2020-11-28 13:13

    Yes. One way to handle this situation is to have each thread lock a shared mutex before accessing the same set object. Make sure you use RAII techniques to lock and unlock the mutex.

提交回复
热议问题