GIL in alternative implementations

Daniel Kluev dan.kluev at gmail.com
Fri May 27 22:06:43 EDT 2011


> So I'd like to know: how do these other implementations handle concurrency
> matters for their primitive types, and prevent them from getting corrupted
> in multithreaded programs (if they do) ? I'm not only thinking about python
> types, but also primitive containers and types used in .Net and Java VMs,
> which aren't atomic elements either at an assembly-level point of view.

Well, they definitely have some shortcomings:

test.py:

from threading import Thread
class X(object):
    pass
obj = X()
obj.x = 0

def f(*args):
   for i in range(10000):
       obj.x += 1

threads = []
for i in range(100):
    t = Thread(target=f)
    threads.append(t)
    t.start()

for t in threads:
    while t.isAlive():
        t.join(1)

print(obj.x)

> python test.py
1000000
> pypy test.py
1000000
> jython-2.5 test.py
19217
> ipy test.py
59040

Not that this thing is reasonable to do in real code, but cpython and
other implementations with GIL at least give you some safety margin.

-- 
With best regards,
Daniel Kluev



More information about the Python-list mailing list