I have a publisher thread and a consumer thread. They share data via a std::stack<Data *>. The publisher simply push() the pointer and consumer simply pop() the pointer, use it and call delete on it. Since there is only single thread publishing pointers one at a time, and one thread consuming pointers, is there any need to synchronize the stack? Keep in mind that stack is only storing pointers. Publisher pushes pointer only when Data() is fully constructed.
I have a publisher thread and a consumer thread. They share data via a
Share
Failure to synchronize on non-
constmethods of containers instdnamespace is undefined behavior.Neither
pushnorpopisconston the underlying container of astack. So two threads are both writing to the state of the underlying container of thestack.A way to think about it is that both are, at the very least, going to have to fight over the count of the number of elements in the
stack: one is trying to increase it, the other is trying to decrease it. (There are other problems, but that one should convince you that both are writing to the state of thestack)