2
嘿,我不能(通过马克·鲁茨编程的Python从)了解下面的代码的某些部分:Python的线程
import _thread as thread
stdoutmutex = thread.allocate_lock()
exitmutexes = [thread.allocate_lock() for i in range(10)]
def counter(myId, count):
for i in range(count):
stdoutmutex.acquire()
print('[%s] => %s' % (myId, i))
stdoutmutex.release()
exitmutexes[myId].acquire()
# signal main thread
for i in range(10):
thread.start_new_thread(counter, (i, 100))
for mutex in exitmutexes:
while not mutex.locked(): pass
print('Main thread exiting.')
我使用Python3。嗯,我能理解stdoutmutex的事情,它是如何工作的,但我无法理解上面的代码如何处理exitmutexes名单,因为在exitmutexes个人锁已被收购,但没有公布。一旦所有10个线程都开始运行,下面的3条线路如何工作?笔者曾形容,这是),而无需使用time.sleep去的方式(
for mutex in exitmutexes:
while not mutex.locked(): pass
print('Main thread exiting.')
获取锁是否使线程保持活动状态? – kaiseroskilo 2012-02-27 10:50:00