basic python threading question

jay.krell at cornell.edu jay.krell at cornell.edu
Sun Oct 1 02:19:50 EDT 2000


Two points:

> thread safety of reference counting

Python should use Interlocked/InterlockedDecrement on Win32.
Disassemble them to get them for other platforms that use Mips, PowerPC,
x86, Alpha (and maybe more if you have Windows CE binaries).
Gcc should provide these..
The shouldn't be too hard to write for other platforms in assembly, at least
single processor platforms. Write the C code for += 1, compile optimized.
Disassembly. Verify it is one instruction, copy it and make it source so you
can guarantee it is one instruction. I think as long as a read/modify/write
is one instruction it is guaranteed thread safe on most _single_ processor
systems. Definitely not on multiprocessor systems though.
You can probably mine the Linux or BSD kernels for this sort of thing too..

> thread safety of immutable objects (other than their refcount)

You've heard of "multiple reader, single writer" locks? Well, think of
immutability as "write once, before any reads, then multiple readers, zero
writers". Function programming fans might point out that this is how much of
their code behaves, and so functional programming is automatically thread
safe. It is key that the one write completes before any other thread can
read the object.

 -  Jay






More information about the Python-list mailing list