Re:Data races in Python, despite the Global Interpreter Lock
by Kranar on Monday February 21, 2022 @07:58PM
(#30422863)
The GIL helps users protect against data races, it does not protect against race conditions. In Python, due to the GIL, it is not possible to concurrently access a memory location and hence there is no concurrent access to x. There are concurrent threads of execution, but they are interrupted either before accessing x, or after accessing x. At no point is a thread of execution interrupted right in the middle of an access to x.
[ Parent
| on HN ]
Re:Data races in Python, despite the Global Interpreter Lock
by verdagon on Monday February 21, 2022 @08:02PM
(#30422886)
These things are running concurrently with each other though. Concurrent operations can happen one after the other, and Python's threads are more about concurrency than parallelism. If these definitions instead mentioned "simultaneous" or "in parallel", I could agree with your belief.
(Edited in response to your edit)
Re:Data races in Python, despite the Global Interpreter Lock
by Kranar on Monday February 21, 2022 @09:05PM
(#30423261)
There need not be any parallelism or simultaneity for a data race to occur. What matters is that writes occur concurrently with another read/write, not whether they occur in parallel.
For example it's possible on a single core CPU to have one thread operate on the higher bits of a 32-bit memory location then yield to second thread that operates on the lower 32-bits of the same memory location so that the end result is a clobbered write. At no point do the two threads run simultaneously and yet this would still be an example of a data race.
Python's GIL makes such an access impossible on any platform it runs on.