mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-18 06:30:23 -04:00
add safe thread example
This commit is contained in:
parent
33e78657eb
commit
e686184be1
1 changed files with 24 additions and 0 deletions
24
Concurrence_examples/safe_thread_example.py
Normal file
24
Concurrence_examples/safe_thread_example.py
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue