#encoding=gbk
import threading
import time
counter=0
class add_thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global counter
time.sleep(1)
counter += 1
print "%s,set counter:%s" %(self.name,counter)
if __name__ == "__main__":
for i in range(200):
thread = add_thread()
thread.start()
A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. In Python, it is currently the lowest level synchronization primitive available, implemented directly by the thread extension module.
按照描述,这个Lock是Thread的最低级别的同步实现,下面还有一段话:
A primitive lock is in one of two states, “locked” or “unlocked”. It is created in the unlocked state. It has two basic methods, acquire() and release(). When the state is unlocked, acquire() changes the state to locked and returns immediately. When the state is locked, acquire() blocks until a call to release() in another thread changes it to unlocked, then the acquire() call resets it to locked and returns. The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised.
#encoding=gbk
import threading
import time
counter=0
mutex=threading.Lock()
class add_thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global counter,mutex
time.sleep(1)
if mutex.acquire():
counter += 1
print "%s,set counter:%s" %(self.name,counter)
mutex.release()
if __name__ == "__main__":
for i in range(200):
thread = add_thread()
thread.start()
A reentrant lock is a synchronization primitive that may be acquired multiple times by the same thread. Internally, it uses the concepts of “owning thread” and “recursion level” in addition to the locked/unlocked state used by primitive locks. In the locked state, some thread owns the lock; in the unlocked state, no thread owns it.
A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default. (Passing one in is useful when several condition variables must share the same lock.)