Python Multitasking Programming - Thread Lock Deadlock

concept



Deadlock: The program cannot continue to run because the lock of a thread is not released in time. It refers to a phenomenon in which two or more processes or threads are waiting for each other due to competition for resources during the execution process. If there is no external force, they will not be able to advance. At this time, the system is said to be in a deadlock state or the system has a deadlock, and these processes that are always waiting for each other are called deadlock processes.


case
import threading
import time

# create mutex
lock = threading.Lock ( ) _


# Take the value according to the subscript to ensure that only one thread can take the value at the same time
def get_value ( index ):

# lock
lock.acquire ( ) _
print ( threading.current_thread ( ) ) _
my_list = [ 3 , 6 , 8 , 1 ]
# Judge subscript release out of bounds
if index >= len ( my_list ):
print ( "Subscript out of bounds:" , index )
return
value = my_list [ index ]
print ( value )
time.sleep ( 0.2 ) _ _
# release lock
lock.release ( ) _


if __name__ == '__main__' :
# Simulate a large number of threads to perform value operations
for i in range ( 30 ):
sub_thread = threading . Thread ( target = get_value , args = ( i ,))
sub_thread.start ( ) _ _


operation result



analyze
In the above program, in the get_value function, because the subscript is out of bounds, the function is directly ended, and the following lock.release () command is not executed. The lock is always in a locked state, other threads cannot be executed, and the program has been waiting to run. .



Solution:


Release the lock in place

import threading
import time

# create mutex
lock = threading.Lock ( ) _


# Take the value according to the subscript to ensure that only one thread can take the value at the same time
def get_value ( index ):

# lock
lock.acquire ( ) _
print ( threading.current_thread ( ) ) _
my_list = [ 3 , 6 , 8 , 1 ]
if index >= len ( my_list ):
print ( "Subscript out of bounds:" , index )
# When the subscript is out of bounds, the lock needs to be released, so that the following thread can still take the value
lock.release ( ) _
return
value = my_list [ index ]
print ( value )
time.sleep ( 0.2 ) _ _
# release lock
lock.release ( ) _


if __name__ == '__main__' :
# Simulate a large number of threads to perform value operations
for i in range ( 30 ):
sub_thread = threading . Thread ( target = get_value , args = ( i ,))
sub_thread.start ( ) _ _



Copyright statement: The content of this article is contributed by Alibaba Cloud's real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00