What's an "atomic" operation (in a threaded context)?

Michael Chermside mcherm at mcherm.com
Thu Nov 13 07:54:30 EST 2003


> This is what's more interesting: object access is threadsafe in the sense that
> you can't get Python's internal data structures into an inconsistent state via
> pure Python code (or, for that matter, well-behaved C extensions). IOW, the
> above code won't ever crash the interpreter or screw up Python's internal
> state. Even if two different threads did tss[5] = <somevalue> (both setting a
> value using the same key) you wouldn't get an inconsistent internal state,
> although you would obviously have no guarantees about the ordering of the
> operations - you'd never know if thread A or thread B changed it last.
> 
> > Can anyone clarify this for me? I'd like to avoid littering my
> > threaded code with huge numbers of unnecessary locks...
> 
> The above applies to CPython - I'm not too sure about Jython.

The same is true for Jython. In Jython, individual python bytecodes are
NOT performed atomically, but there are locks to protect all of the
built-in data types (lists, dicts, etc), so that concurrent access can
never leave it in an inconsistant state.

-- Michael Chermside






More information about the Python-list mailing list