Synchronization Using Lock

Report a typo

Suppose you make transactions in your wallet, only one at a time. First, you need to create a function that adds to the balance, and the other to pay the bills. Create and start the threads:

from threading import Thread, Lock

l = Lock()
wallet = 100


def add_balance(value):
    global wallet
    for i in range(100):
        l.acquire()
        wallet = wallet + value
        l.release()
    print(f"The New balance is: {wallet}")


def pay_bill(value):
    global wallet
    for i in range(100):
        l.acquire()
        wallet = wallet - value
    print(f"The New balance is : {wallet}")


# creating threads
t1 = Thread(target=add_balance, args=(20,))
t2 = Thread(target=pay_bill, args=(5,))
t3 = Thread(target=pay_bill, args=(10,))

# starting threads
t1.start()
t2.start()
t2.start()

The previous code block tries to accomplish this goal but has some bugs. Select the true options.

Select one or more options from the list
___

Create a free account to access the full topic