Multithreading and locking

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Sep 18 12:36:01 CEST 2003


"Ivan Voras" <ivoras at fer.hr> wrote in news:bkbv5l$kgl$1 at bagan.srce.hr:

> 
> Is it true what I heard (as a "rumour" of sorts), that in multithreaded
> Python programs global variables are already automagically protected by
> mutexes? Can someone clarify on that?
> 
The Python interpreter holds a global mutex which is released by some 
methods that call the OS, and is also released sometimes between bytecode 
instructions.

The effect of this is that any operation that takes a single bytecode 
instruction, and which does not call the OS, cannot be interrupted by 
another Python thread. It is left to the user to work out when this means a 
multithreaded operation will be safe.

For example:

    x.extend(y)

If x is a list, then this is safe in a multithreaded environment. If x is a 
user defined class and the extend method is coded in Python, then this 
probably isn't safe.

   x += y

If x and y are lists, this appears superficially to be the same as the 
first example, however this code compiles to two bytecode instructions (an 
inplace add and a store), so it could be interrupted with unfortunate 
consequences.

The bottom line is that with care you can use a list for multi-threaded 
queue operations, but with a Queue class already there and waiting it isn't 
usually worth the risk.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list