Skip to content

Instantly share code, notes, and snippets.

@mmkhitaryan
Created July 4, 2021 19:35
Show Gist options
  • Save mmkhitaryan/bb3e71f9ff4d50dc617497eed15e523b to your computer and use it in GitHub Desktop.
Save mmkhitaryan/bb3e71f9ff4d50dc617497eed15e523b to your computer and use it in GitHub Desktop.
An example of python deadlock
from threading import Lock, Thread
accountone = Lock()
accounttwo = Lock()
def transfer(accountone, accounttwo):
accountone.acquire()
accounttwo.acquire()
print("Transaction done")
accountone.release()
accounttwo.release()
def transfer_do(accountone, accounttwo):
while True:
transfer(accountone, accounttwo) # send money from first account to second
transfer(accounttwo, accountone) # send money from second account to first
for x in range(30):
t = Thread(target=transfer_do, args=(accountone, accounttwo))
t.start()
classical example of banking deadlock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment