Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Python threading Lock Object

I am reading Python Threading Lock API.
Link here.

I am wondering what does the statement below mean.

"When the state is locked, acquire() blocks until a call to release() in another thread changes it to unlocked, then the acquire() call resets it to locked and returns."

I have read this statement 100 times. Still can't get it.

Why another thread to unlock? What does the statement after 'then' mean?

Is there any good explanation for this?

like image 360
DongukJu Avatar asked Apr 25 '26 19:04

DongukJu


1 Answers

The overall meaning is that when a lock is acquired from a thread, other threads that call acquire waits untill the lock is released and lock it again:

When the state is locked,

Thread A acquire() the lock

acquire() blocks

Thread B try to acquire() the lock, but it is locked, so acquire() block current thread

until a call to release() in another thread changes it to unlocked

until the lock is released somewhere else (ie, it waits untill thread A release() the lock)

then the acquire() call resets it to locked and returns.

the acquire() of thread B locks the lock again

like image 96
Gsk Avatar answered Apr 28 '26 08:04

Gsk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!