Question about Python threads

Artur Biesiadowski abies at pg.gda.pl
Tue Aug 27 18:30:20 EDT 2002


Gerhard Häring wrote:

> In Python, everything is an object, including ints, ... so two threads
> manipulating the refcount of an object at the same time will certainly
> crash the interpreter sooner or later. AFAIK Java objects don't have
> refcounts, right?

No, it does not use refcounts, it is not a good idea for anything else 
than niche languages or special objects (for example strings).

JVM has quite well defined semantics in presence of multiple threads 
(including multiple processors). There are some small problems with 
multiprocessor cache and synchronization - java memory model is in 
process of being relaxed to allow optimized implementation.

Anyway, in java, you can modify anything from as many threads as you 
like. Of course, without synchronization, effect of operation is 
unexpected - but result will be legal. This means that if two threads 
modify int field =1 by adding 1 to it, you can get 3 or 2 as a result, 
but not 1, -1 or JVM crash.

For object pointers it is not a problem at all - they are just 
overwritten by competing threads, but everything still works, as 
referencing/dereferencing object does not imply any other actions done 
by JVM.

Of course there are few tricky parts, especially concerning GC. If GC 
tries to run in presence of mutating threads, it potentially COULD crash 
JVM. But this is illegal and no JVM behaves like that. Simpliest 
solution is to stop all threads (either in place or by advancing them to 
special trap points) and only then performing GC (so GC is 
single-threaded on one processor - which is a problem on multi-way 
boxes, performance wise). More complicated solutions include 
generations, marking written pages of memory, etc.

Generally, JVM is not allowed to crash in ANY situation. Of course, real 
life is less perfect and JVMs are known to crash sometimes - but this is 
mostly when interacting with native libraries, performing some kind of 
complicated graphics or working with gigabytes of memory used.


Artur




More information about the Python-list mailing list