The Use of Thread Locks | Teach You to Get Started With Python One Hundred and Two

The Use of Thread Locks | Teach You to Get Started With Python One Hundred and Two

The Use of Thread Locks. When multiple threads modify a shared data almost simultaneously, synchronization control is required. Synchronization is coordinated pacing, running in a predetermined sequence. Thread synchronization can ensure that multiple threads access competing resources safely. The simplest synchronization mechanism is the introduction of mutex locks.
Use of thread locks

The Use of Thread Locks.Synchronize


When multiple threads modify a shared data almost simultaneously, synchronization control is required. Synchronization is coordinated pacing, running in a predetermined sequence. Thread synchronization can ensure that multiple threads access competing resources safely. The simplest synchronization mechanism is the introduction of mutex locks.
Mutex
The Use of Thread Locks.Mutexes introduce a state for a resource: locked/unlocked
When a thread wants to change the shared data, it is locked first. At this time, the state of the resource is "locked", and other threads cannot change it; until the thread releases the resource and changes the state of the resource to "unlocked", other threads can Lock the resource again. Mutual exclusion locks ensure that only one thread performs the write operation at a time, thus ensuring the correctness of data in the case of multi-threading.
The Lock class is defined in the threading module, which can easily handle locking:
# create lock
mutex = threading.Lock ()
# lock
mutex.acquire ()
# freed
mutex.release ()
Notice:
•If the lock was previously unlocked, acquire will not block
•If it is already locked by another thread before calling acquire to lock the lock, then acquire will block until the lock is unlocked.
•Like file operations, Lock can also be opened and closed quickly using the with statement.
Using Mutexes to Solve the Problem of Selling Tickets
import threading
import time

ticket = 20

# create a lock
lock = threading.Lock ()


def sell_ticket ( ) :
global ticket
while True :
print ( ' Hehehe ' )
print ( ' hahaha ' )
print ( ' ddd ' )
print ( ' ppp ' )
print ( ' sss ' )
print ( ' ttt ' )
print ( 'xxx' )
lock.acquire () # add synchronization lock
if ticket > 0:
time.sleep (1)
ticket -= 1
lock.release ()
print ( '{}Sold a ticket, {} remaining' .format( threading.current_thread ().name, ticket))
else :
lock.release ()
print ( 'tickets sold out' )
break


t1 = threading.Thread ( target = sell_ticket , name = 'thread 1' )
t2 = threading.Thread ( target = sell_ticket , name = 'thread2' )

t 1.start ()
t 2.start ()
Locking process:
When a thread acquires the lock by calling the lock's acquire() method, the lock enters the "locked" state.
Only one thread can acquire the lock at a time. If another thread attempts to acquire the lock at this time, the thread will become "blocked", called "blocked", until the thread that owns the lock calls the lock's release() method to release the lock, and the lock enters the "unlocked" state .
The thread scheduler selects one of the threads in the synchronized blocking state to acquire the lock and puts the thread into the running state.
Summarize
The benefits of locks:
ensure that a certain piece of critical code can only be completely executed from start to finish by one thread
Disadvantages of locks:
•It prevents multi-threaded concurrent execution, and a certain piece of code containing locks can only be executed in single-threaded mode, and the efficiency is greatly reduced.
•Since there can be multiple locks, when different threads hold different locks and try to acquire the locks held by each other, a deadlock may result.

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