Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Vector, push_back from another thread crashing?
    primarykey
    data
    text
    <p>I have unexpected assertions faillures in my code using a checked STL implentation. </p> <p>After some research, I narrowed down the problem to a push_back in a vector called from a different thread than the one in which the vector was created.</p> <p>The simplest code to reproduce this problem is : </p> <pre><code>class SomeClass { private: std::vector&lt;int&gt; theVector; public: SomeClass () { theVector.push_back(1); // Ok } void add() { theVector.push_back(1); // Crash } }; </code></pre> <p>The only difference is that SomeClass is instanciated from my main thread, and add is called from another thread. However, there is no concurency issue : in the simplest form of code I used for troubleshooting nobody is reading or writing from this vector except the cases I mentioned above. </p> <p>Tracing into the push_back code, I noticed that some methods from std::vector like count() or size() return garbage, when it's called from the other thred (method "add"), and correct values when called from the creating thread (in the constructor for example)</p> <p>Should I conclude that std::vector is not usable in a multithreaded environement ? Or is there a solution for this problem ? </p> <p>EDIT : removed volatile</p> <p>EDIT 2 : Do you think it's possible that the problem doesn't lie in multithread ? In my test run, add is called only once (verified using a break point). If I remove the push_back from the constructor, I still crashes. So in the end, even with only one call to a vector's method, in a function called once make the assertion fail. Therefore there can't be concurency, or ... ? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload