I was trying to write my first code using python thread.
Please see the code
There is an issue when i release lock for a thread using release() as it says the lock is still availabe [locked() returns True even after release()]
class thread (threading.Thread):
print "the thread %s want to take a lock now" %self.name
print "lock status just after lock acquire foe",self.name,self.lock.locked()
print "release lock for %s" %self.name
print "lock status after lock release is",self.name,self.lock.locked()
the thread T1 want to take a lock now
the thread T2 want to take a lock now
lock status just after lock acquire foe T2 True
release lock for T2
lock status just after lock acquire foe T1 True
lock status after lock release is T2 True
release lock for T1
lock status after lock release is T1 False
When you pass a lock object to the constructor, it isn't getting copied, so each thread object has access to the very same lock.
This results in
True because the lock is still locked, but this time it has been locked by another thread.
This is what actually happens:
>>> import threading >>> lck = threading.Lock() # the 'original' >>> lck2 = lck # lck2 isn't a copy of lck! >>> lck.acquire() True >>> lck2.locked() # lck2 has been acquired as well as lck True >>> lck2.release() >>> lck.locked() # lck has been released as well as lck2 False
Again, this happens because lock objects aren't (and cannot be) copied around and whatever variable is assigned to some lock, it will point to the original object.