I am using std::vector as shared data in a multi-threaded application. I encapsulate the thread inside a class, e.g.,
class ABC {
public:
double a, b, c
Actually, it is absolutely pointless to state X
is or is not thread-safe! You need to qualify for what kind of uses. For example, hardly any class will be "thread-safe" when it is somehow used in one thread and destroyed in another.
That said, the statement that std::vector<T>
is not thread- safe, independent of how often it is repeated, is wrong. However, it seems most people neither understand nor appreciate the thread-safety guarantees given. std::vector<T>
is thread-safe in the following sense:
This applies to vector structure itself. Accesses to contained object are bound to whatever rules are imposed on them. These are apparently not the thread-safety guarantees many people have in mind but anything stronger won't work with the container interface.
You call ptrVector->size()
without locking it first. This could easily be the cause of your problems. Make sure to lock your vector before any reads or writes.
Maybe you could use this instead?
concurrent_vector... from the Intel Threading Building Blocks
https://software.intel.com/en-us/node/467758