Re: std::deque Thread Saftey Situtation
On Sep 1, 9:34 am, NvrBst <nvr...@gmail.com> wrote:
No - you are not correct. Imagine a situation where the
pushing thread pushes the object to the dequeue and then
increases the counter. However, the size get written
through the cache whereas the object stays (partly) in the
cache. Now the reader will see that there is an object in
the dequeue, but in reality it was never written to a
location that allows the other thread to see it (it is on
another core), so it reads rubbish where the object should
be.
I don't quite understand what you say ("on another core"
part), but I'm pretty sure it is safe. The pop'ing thread is
only taking the first element, and the push'ing thread is
appending to the end.
I don't have the original code handy to verify, but what you
describe above is only safe if all accesses are synchronized
somehow.
The size isn't incremented until after the object is written,
In the thread which is pushing. Unless specific precautions are
taken (external synchronization at the system level, or fence or
membar instructions), different threads are not guaranteed to
see memory writes in the same order.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34