Is std::vector thread-safe and concurrent by default? Why or why not?

后端 未结 2 1175
天命终不由人
天命终不由人 2020-12-19 02:58

What does it mean to make a dynamic array thread-safe and concurrent? Say, for example, std::vector.

  1. Two threads may want to insert at the same po
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 03:44

    The only concurrent operations on a single object in the standard library that are safe by default are - Only accessing const-member functions - All accesses to synchronization primitives (like mutex lock and unlock or atomic operations) Everything else has to be externally synchronized. In particular, the standard library doesn't have any thread safe containers yet (as of c++14)

    So the answer to both of your examples is no, they both require a form of external synchronization.

    What you can do of course is modifying the value of two different elements in the container.

提交回复
热议问题