[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Thread-safe way to add a key to a dict only if it isn't already there?

Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
> On Sun, 08 Jul 2018 14:11:58 +0300, Marko Rauhamaa wrote:
>> PS My example with "impossible" being the result of a racy integer
>> operation is of course unlikely but could be the outcome if the Python
>> runtime reorganized its object cache on the fly (in a hypothetical
>> implementation).
> That would be a cache bug :-)
> Not every interpreter bug should be considered the caller's fault :-)

Whether it's a bug or not depends on the language specification. Java
has a very clear definition for correct synchronization. It's not so
clear on what could happen in the event of a data race; it's not
entirely unspecified, but the language spec admonishes the application
to brace itself for surprising effects.

If the Python Language Specification hasn't specified the effects of
incorrect synchronization so we can rightly suppose that the results are