"""Example of lock acquisition blocking forever on Linux. Expected output is something like this: In run(), the number is: 1 In run(), the number is: 2 In run(), the number is: 3 In print_number(), the number is: 3 In run(), the number is: 4 On Linux you get this: In run(), the number is: 1 In run(), the number is: 2 In run(), the number is: 3 In run(), the number is: 4 In run(), the number is: 5 ... going on forever """ import threading import time class MyThread(threading.Thread): def __init__(self): super(MyThread, self).__init__() self.running = True self.lock = threading.RLock() self._number = 0 def run(self): while self.running: #time.sleep(0.001) #Uncomment to avoid hang on Linux with self.lock: self._number += 1 print "In run(), the number is:", self._number time.sleep(0.1) def print_number(self): with self.lock: print "In print_number(), the number is:", self._number return self._number t = MyThread() t.start() time.sleep(0.3) t.print_number() t.running = False