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

Marko Rauhamaa marko at
Sun Jul 8 14:57:08 EDT 2018

Chris Angelico <rosuav at>:

> On Mon, Jul 9, 2018 at 2:11 AM, Marko Rauhamaa <marko at> wrote:
>> MRAB <python at>:
>>> In C you'd declare 'quit' as 'volatile' to tell the compiler that it
>>> could change unexpectedly, so don't make that assumption.
>> C is an even tougher case. Even if the compiler kept on checking a
>> volatile value, the CPU might never propagate the cache content to
>> the other core. You'd need a memory barrier. In Java, "volatile"
>> effectively creates a memory barrier, but in C (and C++) it does not.
>> In C you need something like a mutex to see the effects of other
>> threads running.
>> (BTW, I think that's a terrible thing for the C standards committee to
>> specify.)
> None of this has any impact on Python whatsoever.

[citation needed]


More information about the Python-list mailing list