在看programing python 4th,第5張parallel system tool 192頁開始,書中講到thread知識,如下作個筆記,以便後期學習python
1.主線程執行,開啓5個子線程進行計數,沒有使用mutex鎖住,因此線程沒有lock住資源,每一個線程對全局變量的操做錯亂,結果以下:ide
1 """ 2 synchronize access to stdout: because it is shared global 3 thread outputs may be intermixed if not syschronized 4 """ 5 import thread,time 6 global num #global var to be used by many threads 7 num=0 8 9 def cnt(id,count): # function run in threads 10 for i in range(count): 11 global num 12 #mutex.acquire() # lock the share var before execute 13 num +=1 14 time.sleep(0.5) # simulate read work 15 print('[%s] num= %s\n' %(id,num)) #print isn't interrupted now 16 #mutex.release() #release the lock for the other thread 17 18 if __name__ =="__main__": 19 #mutex=thread.allocate_lock() #make a global mutex for lock 20 for i in range(5): #spawm 5 threads 21 thread.start_new_thread(cnt,(i,3)) #start threads 22 time.sleep(8) # wait for spawn thread work done,don't exit too early 23 24 print('main thread exitting')
2.把mutex 註釋打開,有了mutex變量,每個線程進入都會獨佔num變量,結果以下:學習
1 """ 2 synchronize access to stdout: because it is shared global 3 thread outputs may be intermixed if not syschronized 4 """ 5 import thread,time 6 global num #global var to be used by many threads 7 num=0 8 9 def cnt(id,count): # function run in threads 10 for i in range(count): 11 global num 12 mutex.acquire() # lock the share var before execute 13 num +=1 14 time.sleep(0.5) # simulate read work 15 print('[%s] num= %s\n' %(id,num)) #print isn't interrupted now 16 mutex.release() #release the lock for the other thread 17 18 if __name__ =="__main__": 19 mutex=thread.allocate_lock() #make a global mutex for lock 20 for i in range(5): #spawm 5 threads 21 thread.start_new_thread(cnt,(i,3)) #start threads 22 time.sleep(8) # wait for spawn thread work done,don't exit too early 23 24 print('main thread exitting')
3.若是把time.sleep(6)註釋掉或者子線程沒有執行完畢,而主線程sleep的時間一到,主線程直接退出而不等待子線程執行完畢,結果以下:ui
a.主線程不等待,則直接退出spa
b.主線程只等待3s,而5個子線程須要7.5s,因此num只計數5.線程
4.設定有效等待時間和鎖以後,主線程等待全部子線程執行結束才退出,結果以下:3d
6.無需在主線程設置等待時間,而是設定單獨的鎖或者變量來記錄每一個子線程的執行狀態,每執行完一個線程,設定狀態鎖,而後在主線程判斷全部狀態鎖的狀態便可code
1 """ 2 used mutexex to know when threads are done in parent/main thread, 3 instead of time.sleep;lock stdout to avoid comingled prints 4 """ 5 import thread,time 6 global num 7 num =0 8 9 def cnt(id,count): 10 for i in range(count): 11 global num 12 stdoutmutex.acquire() 13 num +=1 14 time.sleep(0.5) 15 print('[%s] num= %s time:[%s]\n' %(id,num,time.ctime())) #print isn't interrupted now 16 stdoutmutex.release() 17 #exitmutexs[id].acquire() # signal main thread 18 exitFlags[id] = True #signal main thread 19 20 if __name__ =="__main__": 21 stdoutmutex = thread.allocate_lock() #make a global mutex for lock 22 #exitmutexs = [thread.allocate_lock() for i in range(5)] 23 exitFlags=[False]*5 24 for i in range(5): #spawm 5 threads 25 thread.start_new_thread(cnt,(i,3)) #start threads 26 #for mutex in exitmutexs: 27 # while not mutex.locked(): 28 # pass 29 while False in exitFlags:pass 30 print('main thread exitting')