[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?

Chris Angelico <rosuav at gmail.com>:

> On Mon, Jul 9, 2018 at 2:11 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> MRAB <python at mrabarnett.plus.com>:
>>> 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]