atomic { tmp = x + 1 }
atomic { x = tmp }
atomic { tmp = y - 1 }
atomic { y = tmp }
In fact I would expect this is the most likely possibility, at least for user defined objects. atomic tmp=x
atomic tmp+=1
atomic x=tmp
atomic tmp=y
atomic tmp-=1
atomic y=tmpThat sounds good and authoritative. Unfortunately the Python documentation itself is silent on the subject. And more importantly the Python community is filled with people who believe exactly the opposite.
It's a real hole. And the only solution right now is to tell everyone to avoid threads. The Python world simply isn't prepared for them.
FWIW: I'm one of a handful of people who have found and fixed race conditions in python scripts. And it was largely because my background is in racey C code and my eyes were primed to see it. And even then, I genuinely thought (based on incorrect advice like the above) that this sort of thing was impossible and kept looking before trying the obvious synchronization solution. And it was only then that I realized what a mess concurrency is in the mind of the modern python developer.
I used to think that it did as well, but then my C/Java brain kicked in and realized that couldn't be correct. I wrote this article to help others see it in action.
I did too. I even wrote multithreaded test programs to see if it was true: they would always give me consistent results on Python and Ruby but on Java and C data got clobbered as expected.
essentially the same operations in the same order, that need to be isolated (i.e. transactional / atomic) from concurrent threads performing the same operations.
Obviously, the fundamental issue with concurrency is programmer’s intent. This statement:
can be interpreted in two ways: or The best the compiler can do would be to alert the programmer of the ambiguity; I know of no compiler that does that.