With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe
I wanted to know if the following:
(x, y) = (y, x)
will be guaranteed atomic in cPython. (x and y are both python variables)
Let’s see:
It doesn’t appear that they’re atomic: the values of x and y could be changed by another thread between the
LOAD_GLOBALbytecodes, before or after theROT_TWO, and between theSTORE_GLOBALbytecodes.If you want to swap two variables atomically, you’ll need a lock or a mutex.
For those desiring empirical proof: