import threading
import time
class Zaehler(threading.Thread):
einaus = threading.Lock()
def __init__(self):
super().__init__()
self.count = 1
def run(self):
for a in range(10):
#Critical Section
with Zaehler.einaus:
self.count += a
print(self.count)
#Critical Section
meine_threads = []
thread = Zaehler()
thread2 = Zaehler()
#thread3 = Zaehler()
meine_threads.append(thread)
meine_threads.append(thread2)
#meine_threads.append(thread3)
thread.start()
thread2.start()
#thread3.start()
for t in meine_threads:
t.join()
OUTPUT:
1
2
4
7
11
16
22
29
37
46
1
2
4
7
11
16
22
29
37
46
Process finished with exit code 0
This program has only one goal. To understand Locking() :). The variable count is the shared resource which the threads are outputing after the counted the variable up. But instead of giving ( 1, 2, 3, 4, 5, 6 ....) I get the output (1, 2, 4, 7, 11, 16, 22 )