Threading with locks : lock « Thread « Python Tutorial






import threading, time

b = 50

l = threading.Lock()

def threadcode():
    global b
    print "Thread %s invoked" % threading.currentThread().getName()

    l.acquire()
    try:
        print "Thread %s running" % threading.currentThread().getName()
        time.sleep(1)
        b = b + 50
        print "Thread %s set b to %d" % (threading.currentThread().getName(),b)
    finally:
        l.release()

print "Value of b at start of program:", b

childthreads = []

for i in range(1, 5):
    t = threading.Thread(target = threadcode, name = "Thread-%d" % i)

    t.setDaemon(1)

    t.start()
    childthreads.append(t)

for t in childthreads:
    t.join()

print "New value of b:", b








17.7.lock
17.7.1.Using thread and Locks
17.7.2.Threading with locks