<gh_stars>0
import time, threading

balance = 0
lock = threading.Lock()

def change_id(n):
    global balance
    balance = balance + n
    # balance = balance - n


def run_thread(n):
    for i in range(10):
        lock.acquire()
        try:
            change_id(i)
        finally:
            lock.release()


t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))

t1.start()
t2.start()
t1.join()
t2.join()
print(balance)


import time, threading

balance = 0
lock = threading.Lock()

def change_id(n):
    global balance
    balance = balance + n
    # balance = balance - n


def run_thread(n):
    for i in range(10):
        lock.acquire()
        try:
            change_id(i)
        finally:
            lock.release()


t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))

t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
# balance = balance - n