#!/usr/bin/python
#-*- coding:UTF-8 -*-
'''
Created on 2015-9-14
@author: huangpeng03
线程同步---锁
'''
import threading
import time
import thread
class Counter:
def __init__(self):
self.value = 0
self.lock = thread.allocate_lock()
def increment(self):
self.lock.acquire()
self.value = self.value + 1
value = self.value
self.lock.release()
return value
class ThreadDemo(threading.Thread):
def __init__(self, coun):
threading.Thread.__init__(self)
self.c = coun
def run(self):
time.sleep(1)
print self.c.increment()
if __name__ == '__main__':
c = Counter()
for index in xrange(100):
thread = ThreadDemo(coun=c)
thread.start()
Python线程同步--互斥锁
最新推荐文章于 2021-01-14 16:32:20 发布