Which methods in std::vector are thread safe? If none, what data structures are there that support insert, read and replace operations that are thread safe? I'm using c++14
Printable View
Which methods in std::vector are thread safe? If none, what data structures are there that support insert, read and replace operations that are thread safe? I'm using c++14
std::vector actually has no methods in the C++ sense of the word: a method in C++ is another way of saying "virtual member function". But you're probably using the term in the more general object-oriented sense to mean a member function in C++, in which case the answer can be found in the standard (I'm quoting from C++11 because I never did get round to obtaining a copy of C++14):Quote:
Originally Posted by c_weed
To put it another way: the const member functions are thread safe (and the non-const overloads are still "considered const" for this purpose in that merely calling them to read but not modify would be thread safe), but otherwise modifying the individual elements is only "partially thread safe" in that modifying different elements would be thread safe, but modifying the same element wouldn't be thread safe. Oh, and since the copy constructors can be invoked with the argument being in a const context, they are thread safe too.Quote:
Originally Posted by C++11 Clause 23.2.2
As you're using C++14, you can use the facilities from <atomic> or <mutex> to make your use of insert, read and replace operations guaranteed thread safe.Quote:
Originally Posted by c_weed