python中上雙互斥鎖的執行緒執行流程

2022-08-19 17:51:08 字數 1415 閱讀 9019

import threading

def sing():

print('進入sing -----------------')

for i in range(3):

print('進入sing迴圈 -----------------')

l1.acquire()

print('sing上鎖 -----------------')

print('唱歌。。。')

try:

print('dance解鎖 -----------------')

l2.release()

print('dance解鎖成功 -----------------')

except exception:

print('sing中dance解鎖失敗,重新開始迴圈----------------')

pass

def dance():

print('進入dance -----------------')

for i in range(3):

print('進入dance迴圈 -----------------')

l2.acquire()

print('dance上鎖 -----------------')

print('跳舞。。。')

try:

print('sing解鎖 -----------------')

l1.release()

print('sing解鎖成功 -----------------')

except exception:

print('dance中sing解鎖失敗,重新開始迴圈----------------')

pass

if __name__ == '__main__':

# sing()

# dance()

l1 = threading.lock()

print(l1)

l2 = threading.lock()

print(l2)

t1 = threading.thread(target=sing)

t2 = threading.thread(target=dance)

print('執行緒建立完畢--------')

t1.start()

print('執行緒1開始執行------')

t2.start()

print('執行緒2開始執行------')

t1.join()

print('執行緒1守護主線程-----')

t2.join()

print('執行緒2守護主線程-----')

python執行緒互斥鎖 Python多執行緒與互斥鎖

多執行緒 threading python的thread模組是 較底層的模組,python的threading 模組是對thread做了 些包裝的,可以更加 便的被使 1.使 threading模組 from threading import thread 匯入模組 t thread target ...

Python執行緒中的互斥鎖

互斥鎖引入背景 coding utf 8 author bolin chen date 2021 02 22 import threading,time num 0 def th1 temp global num for i in range temp num 1 print f in the th...

執行緒 互斥鎖

include include include include include 1.靜態初始化,當動態初始化時,遮蔽靜態初始化 pthread mutex t mutex pthread mutex initializer 2.動態初始化 pthread mutex t mutex int lock...