I also sleep 2 s before the final output, this phenomenon is what reason, (just contact, the hope can say the detailed point, thank you)
The import threading
The import time
Def test1 (temp) :
Global g_nums
# lock 1
-- -- -- -- --# mutex. Acquire ()
For I in range (temp) :
# print (" -- -- -- -- - the gums={} - ", g_nums)
Lock - # 2
Mutex. Acquire ()
G_nums +=1
Mutex. Release ()
# print (" -- -- -- -- -- - the test1 -- -- -- -- -- \ n ")
# mutex. Release ()
Print (" -- -- -- -- -- in test1 g_num {}, {} ". The format (g_nums, time ctime ()))
Def test2 (temp) :
Global g_nums
Lock - # 3
# mutex. Acquire ()
For I in range (temp) :
# print (" -- -- -- -- - the gums={} - ", g_nums)
Lock - # 4
Mutex. Acquire ()
G_nums +=1
Mutex. Release ()
# print (" -- -- -- -- -- - the test2 -- -- -- -- -- ")
# mutex. Release ()
Print (" -- -- -- -- - in the test2 g_num {} - {} ". The format (g_nums, time ctime ()))
G_nums=0
Nums=10000000
Mutex=threading. The Lock () # create a mutex
Def the main () :
T1=threading. Thread (target=test1, args=(nums,))
T2=threading. Thread (target=test2, args=(nums,))
T1. The start ()
T2. The start ()
Time. Sleep (2)
Print (" -- -- -- -- -- in the main g_num {}, {} ". The format (g_nums, time ctime ()))
If __name__=="__main__" :
The main ()