There are three kinds of actuaries: those who can count, and those who can't.
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.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Stephan van Hulst wrote:Is it correct to say that the update/read of atomic synchronizes the two threads?
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|