Created
July 4, 2021 19:35
-
-
Save mmkhitaryan/bb3e71f9ff4d50dc617497eed15e523b to your computer and use it in GitHub Desktop.
An example of python deadlock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| classical example of banking deadlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment