is < vector > threadsafe for read/write at different locations? is < vector > threadsafe for read/write at different locations? multithreading multithreading

is < vector > threadsafe for read/write at different locations?


vector does not provide any thread-safety guarantees, so technically the answer would be no.

In practice, you should be able to get away with it... until the day that someone (possibly you) makes a small change in one corner of the program and all hell breaks loose. I wouldn't feel comfortable doing this in any non-trivial program.


From MSDN: Thread Safety in the Standard C++ Library

For reads to the same object, the object is thread safe for reading:

  • From one thread at a time when no writers on other threads.
  • From many threads at a time when no writers on other threads.

For writes to the same object, the object is thread safe for writing from one thread when no readers on other threads

For reads to different objects of the same class, the object is thread safe for reading:

  • From one thread at a time.
  • From one thread at a time when no writers on other threads.
  • From many threads at a time.
  • From many threads at a time when no writers on other threads.

For writes to different objects of the same class, the object is thread safe for writing:

  • From one thread when no readers on other threads.
  • From many threads.

So from the above, Theorotically, NO, it won't be threadsafe.


Theoretically: No.

Practically: Yes (According how all well known STLs are implemented)