diff --git a/Concurrence_examples/safe_thread_example.py b/Concurrence_examples/safe_thread_example.py new file mode 100644 index 0000000..e222592 --- /dev/null +++ b/Concurrence_examples/safe_thread_example.py @@ -0,0 +1,24 @@ +import threading + +counter = 0 +threads = [] + +lock = threading.Lock() + + +def count_with_lock(): + + global counter + + for _ in range(100): + with lock: + counter += 1 + + +for _ in range(100): + thread = threading.Thread(target=count_with_lock) + thread.start() + threads.append(thread) + + +print(counter) \ No newline at end of file