Skip to content

Instantly share code, notes, and snippets.

@laixintao
Created July 17, 2018 09:03
Show Gist options
  • Save laixintao/fcfef35fd99d4236612fcc8309c0bd4e to your computer and use it in GitHub Desktop.
Save laixintao/fcfef35fd99d4236612fcc8309c0bd4e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Since we have GIL, should we add Lock for global variable read/write?
"""
import threading
shared_resource_with_no_lock = 0
COUNT = 100000
def increment_without_lock():
global shared_resource_with_no_lock
for i in range(COUNT):
shared_resource_with_no_lock += 1
def decrement_without_lock():
global shared_resource_with_no_lock
for i in range(COUNT):
shared_resource_with_no_lock -= 1
while 1:
t3 = threading.Thread(target=increment_without_lock)
t4 = threading.Thread(target=decrement_without_lock)
t3.start()
t4.start()
t3.join()
t4.join()
print(shared_resource_with_no_lock)
@laixintao
Copy link
Author

It seems Python2.7 are different from Python3:

➜  tmp python3 multi.py
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
^CTraceback (most recent call last):
  File "multi.py", line 31, in <module>
    t4.join()
  File "/usr/local/var/pyenv/versions/3.7.0/lib/python3.7/threading.py", line 1032, in join
    self._wait_for_tstate_lock()
  File "/usr/local/var/pyenv/versions/3.7.0/lib/python3.7/threading.py", line 1048, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
➜  tmp python2.7 multi.py
-893
-2816
24834
27817
27331
44196
51711
73761
82130
73066
46541
73947
97593
88501
94232
92298
82604
114755
122483
112331
147345
157499
164016
165515
173650
174583
176597
184220
177307
139963
133751
131352
132410
122155
103723
106903
115279
139169
139906
166301
196960
184505
206883
210829
192812
199480
191403
198301
184545
207097
180438
166379
178448
208363
221046
226575
210732
222276
254142
252076
254308
275874
304797
298688
329393
347032
370550
374592
367625
^CTraceback (most recent call last):
  File "multi.py", line 30, in <module>
    t3.join()
  File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 940, in join
    self.__block.wait()
  File "/usr/local/Cellar/python@2/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 340, in wait
    waiter.acquire()
KeyboardInterrupt

@laixintao
Copy link
Author

If you waiting long enough you will see same problems happens in Python3


0
0
0
0
0
0
0
0
0
0
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
-40611
^CTraceback (most recent call last):
  File "multi.py", line 31, in <module>
    t4.join()
  File "/usr/local/var/pyenv/versions/3.7.0/lib/python3.7/threading.py", line 1032, in join
    self._wait_for_tstate_lock()
  File "/usr/local/var/pyenv/versions/3.7.0/lib/python3.7/threading.py", line 1048, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
➜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment