4 ms·
Is this true? It looks like += compiles to four bytecode instructions: two loads, an increment, and a store. It should be possible for a thread to get paused af
by hexane360 5y ago
Is this true? It looks like += compiles to four bytecode instructions: two loads, an increment, and a store. It should be possible for a thread to get paused after the load but before the store, resulting in a stale read and lost write.
Some more discussion here: https://stackoverflow.com/questions/1717393/is-the-operator-thread-safe-in-python https://stackoverflow.com/questions/1717393/is-the-operator-...
- dehrmann 5y agoMaybe it's just certain collection operations, then.
- detaro 5y agoAnd the proposal clearly addresses adjusting those to maintain the expected guarantees.
- pansa2 5y agoWith the GIL, for an int i, `i += 1` is not thread-safe, but IIRC for a list l, `l += [1]` (i.e. extend) is. Presumably this patch changes the list implementation in some way so that the extend operation remains thread-safe without the GIL.