''' threads3.py Jeff Ondich, 15 November 2010 Same as threads2.py, but this time uses a lock to protect the critical section. ''' from threading import * class FightingThread(Thread): def __init__(self, symbol): Thread.__init__(self) self.symbol = symbol def run(self): for j in range(100000): gLock.acquire() for k in range(len(gSharedList)): gSharedList[k] = self.symbol if gSharedList != [self.symbol] * len(gSharedList): print self.symbol, ':', gSharedList gLock.release() gSharedList = ['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'] gLock = Lock() threadA = FightingThread('a') threadB = FightingThread('b') threadA.start() threadB.start()