Thread Safety

MetalOne jcb at
Fri Oct 3 20:20:11 CEST 2003

"Martin v. Löwis" <martin at> wrote in message news:<bli0p3$p23$07$1 at>...
> MetalOne wrote:
> > Are atomic operations thread safe in Python?  
> I don't understand the question. Atomic operations, by nature, can
> either completely succeed or completely fail - otherwise it would not
> be atomic. This is independent of Python.
> But then, in Java, strings are not immutable, no?
> Regards,
> Martin

The problem in Java is one of visibility. Assigning 
s = "hello"
does not necessarily happen if < s > is not used in the current

Likewise, another thread may see no reason to examine < s >, if < s >
has not been changed in this thread.

I found the following on a Java site.
# the language definition ensures that single memory moves concerning
32-bit values (int, etc.) are "atomic" (not interrupted)
# moving a 64-bit value (say, long) can be preempted in the middle of
the transfer operation (but it may not!)

# however, the atomicity of 32-bit moves ensures nothing about the
visibility of updated (lastly written) values (see below)

# the atomicity and correct visibility of a scalar (32- or 64-bit)
variable can be ensured by defining it volatile
# a volatile variable is always kept up-to-date and atomically loaded
from and stored into the main memory
# unfortunately, not all current Java VM implementations handle
volatile variables correctly

Java strings are immutable.  There is a StringBuffer class that is

More information about the Python-list mailing list