Piet Souris wrote:That's what 'volatile' was meant for, apart from synchronization issues. Correct or not: this was what I learned.
According to my above simple scenario, I'd say that Thread B in your scenario still might miss the change of 'primitive', but I agree that there is a 'happens before' situation here.
I would say: make 'primitive' volatile, to stay on the safe side.
But: why do you not consider the race condition here? Isn't that at least as important?
Moral of this: testing might not guarantee a definite answer.
I don't want to perform synchronization if I don't have to. Accessing a volatile variable incurs half of the cost of a synchronized block.
Stephan van Hulst wrote:Is it correct to say that the update/read of atomic synchronizes the two threads?