**
单线程中不会存在问题
然而在多线程中,操作系统交叉处理赋值语句,导致
全局变量被一个线程修改,而另一个线程却不知情。
**

#__author:   han-zhang
    #date:  2018/11/23 17:47
    #file:  银行.py
    #IDE:   PyCharm
    
    import threading
    
    money=0
    lock=threading.Lock()
    
    #存钱
    def put_money(sum):
        global money
        money+=sum
    
    #取钱
    def get_money(sum):
        global money
        money-=sum
    
    def run(sum):
        lock.acquire()
        for i in range(1000000):
            put_money(sum)
            get_money(sum)
        lock.release()
    
    '''
    单线程中不会存在问题
    然而在多线程中,操作系统交叉处理赋值语句,导致
     全局变量被一个线程修改,而另一个线程却不知情。
    '''
    
    t1=threading.Thread(target=run,args=(100,))
    t2=threading.Thread(target=run,args=(1000,))
    
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print(money)