2017-04-02 58 views
0

我有競爭條件的Python代碼。如何使用valgrind和Python代碼來檢測競爭狀態?

import threading 
class Counter: 
    def __init__(self): 
     self.x = 0 
    def incr(self): 
     self.x += 1 
    def __str__(self): 
     return str(self.x) 

x = Counter() 

class T(threading.Thread): 
    def run(self): 
     for i in range(100000): 
      x.incr() 

t1 = T() 
t2 = T() 
t1.start() 
t2.start() 
t1.join() 
t2.join() 
print(x) 

我試圖用valgrind來檢測它。

valgrind --tool=helgrind --suppressions=valgrind-python.supp \python -E -tt ./1.py 
0 errors from 0 contexts (suppressed: 9 from 9) 

我可以使用valgrind檢測Python代碼中的競爭條件嗎?

回答

0

Python的標準實現使用全局解釋器鎖來確保一次只有一個線程可以執行Python字節碼。

所以從valgrind的角度來看,只有一個線程正在運行。