[Python-Dev] Threading idea -- exposing a global thread lock

Michael Chermside mcherm at mcherm.com
Tue Mar 14 19:07:40 CET 2006


Josiah Carlson writes:
> It would be nice if Jython or IronPython could (and would) implement
> these 'critical sections'.  Whether they can or not, I think that it
> would be a useful feature in the CPython runtime.

The issue is not whether Jython and IronPython "will", it's whether
they "can". Essentially, Jython and IronPython both use locking within
their fundamental data structures. This then allows them to freely
allow threads to run on multiple processors. Meanwhile, CPython lacks
locks in its fundamental data structures, so it uses the GIL to
ensure that code which might touch Python data structures executes on
only one CPU at a time.

The concept of a "critical section" makes great sense when there is
effectively only one CPU: just stop switching threads. But if code
is using multiple CPUs, what does it mean? Shut down the other CPUs?
To do such a thing cooperatively would require checking some master
lock at every step... (a price which CPython pays, but which the
implementations built on good thread-supporting VMs don't have to).
To do such a thing non-cooperatively is not supported in either VM.

Since I doubt we're going to convince Sun or Microsoft to change
their approach to threading, I think it is unwise to build such a
feature into the Python language. Supporting it in CPython only
requires (I think) no more than a very simple C extension. I think
it should stay as an extension and not become part of the language.

-- Michael Chermside


More information about the Python-Dev mailing list