0

I am reading Python threading module, as a method of the class Condition, __is_owned(self) tests if current thread owns the lock or not:

  def _is_owned(self):                                                                                                                                                               
      # Return True if lock is owned by current_thread.                                                                                                                              
      # This method is called only if __lock doesn't have _is_owned().                                                                                                               
      if self.__lock.acquire(0):                                                                                                                                                     
          self.__lock.release()                                                                                                                                                      
          return False                                                                                                                                                               
      else:                                                                                                                                                                          
          return True  

This test will fail if thread a has the lock, and thread b tries to test is it owns the lock, see the code below:

import thread                                                                                                                                                                            
import time                                                                                                                                                                              

lock = thread.allocate_lock()                                                                                                                                                            
def is_owned(lock):                                                                                                                                                                      
    if lock.acquire(0):                                                                                                                                                                  
        lock.release()                                                                                                                                                                   
        return False                                                                                                                                                                     
    else:                                                                                                                                                                                
        return True                                                                                                                                                                      

def thread1(lock):                                                                                                                                                                       
    lock.acquire()                                                                                                                                                                       
    print 'thread 1 acquired lock, going to block'                                                                                                                                       
    print 'thread 1, lock is owned by me', is_owned(lock)                                                                                                                                
    lock.acquire()                                                                                                                                                                       

def thread2(lock):                                                                                                                                                                       
    time.sleep(5)                                                                                                                                                                        
    print 'thread 2 try to acquire lock, without waiting'                                                                                                                                
    print 'thread 2: lock is owned by me', is_owned(lock)                                                                                                                                

thread.start_new_thread(thread1, (lock,))                                                                                                                                                
thread.start_new_thread(thread2, (lock,))

The outputs are :

In [6]: run is_owned.py
thread 1 acquired lock, going to block
thread 1, lock is owned by me True

In [7]: thread 2 try to acquire lock, without waiting
thread 2: lock is owned by me True

In[7] shows here because of the sleep(5), the second acquire attempt will block the thread indefinitely.

Why is this a test for ownership?

User
  • 14,131
  • 2
  • 40
  • 59
fast tooth
  • 2,317
  • 4
  • 25
  • 34
  • It reminds me of an RLock. http://stackoverflow.com/questions/16567958/when-and-how-to-use-pythons-rlock/16568426#16568426 – User Nov 02 '14 at 06:29
  • Hello. But you can see from the source code that the lock is a normal thread.lock by allocate_lock() How does this relates to rlock? Thanks – fast tooth Nov 02 '14 at 17:55
  • Is your question about usefulness or should sound like this: "Why is this a test for thread ownership?" – User Nov 02 '14 at 18:55
  • My question is why is this a test for ownership. Thanks – fast tooth Nov 02 '14 at 19:32

0 Answers0