In this lesson, you’ll learn about lock objects and how you can use a lock as a context manager to prevent more than one thread from accessing a part of your program at the same time. If you download the sample code, you can get your own copy of 10-lock.py
:
To learn more, you can also check out the documentation.
malbert137 on June 1, 2020
Technically, there is no guarantee that the the “withdraw” thread acquires the lock before the “deposit” thread, but this should be very rare in practice. In real life, the ordering of the deposit and withdraw would be determined by the account holders (e.g., perhaps the account is held by two people, and one is making a deposit at one location while the other is trying to make a withdraw at another location), so the code wouldn’t be sequencing those actions. In any case, there is no check for overdraw.