I am writing code in VS2005 using its STL. I have one user interface thread to read a vector and a workflow to write to the vector. I use :: boost :: shared_ptr as a vector element.
vector<shared_ptr<Class>> vec;
but I find if I manipulate vec in both threads at the same time (I can guarantee that they do not visit the same area, UI Thread always reads the area in which there is information)
vec.clear () seems unable to free the resource. The problem occurs in shared_ptr, it cannot free its resource.
What is the problem? This is due to the fact that when a vector reaches its order capacity, it is redistributed in memory, then the original part becomes invalid.
As far as I know when redistributing, the iterator will be invalid, why the problem occurred when I used vec [i]. // -----------------------------------------------
What locks are needed? I mean: if the vector element is shared_ptr, when thread A receives the smart_p point, will another thread B wait until A completes the operation on smart_p to the right? Or just add a lock when a thread tries to read a point, when read completion is complete, thread B can continue to do something.
source
share