Add files via upload

This commit is contained in:
Lerking
2018-02-20 06:30:18 +01:00
committed by GitHub
parent e311c278e7
commit 81fff6e01c
8 changed files with 515 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
import time
from multiprocessing import Process, Value, Lock
class Counter(object):
def __init__(self, initval=0):
self.val = Value('i', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
def value(self):
with self.lock:
return self.val.value
def func(counter):
for i in range(50):
time.sleep(0.01)
counter.increment()
if __name__ == '__main__':
counter = Counter(0)
procs = [Process(target=func, args=(counter,)) for i in range(10)]
for p in procs: p.start()
for p in procs: p.join()
print (counter.value())