Re-entrant locks

Beyond the threading.Lock class, which provides the general locking mechanism for multithreading, where a lock can only be acquired once until it is released, Python also provides another locking mechanism that might be useful for programs that implement recursive operations. This lock, known as a re-entrant lock and implemented using the threading.RLock class, can be used by recursive functions. The class provides similar methods to those provided by the lock class: acquire() and release()which are to acquire and release the taken locks, respectively. The only difference occurs when a recursive function calls acquire() multiple times across the call stack. When the same function calls the acquire method again and again, a new lock is given to the function. To release this lock, the function needs to call the release method.

One interesting thing to note here is that the lock will be fully released only at the final release() method call made by the recursive method. If some other thread tries to acquire a lock before the final release() call is made, the thread will block on execution and will wait until the lock is freed up from the previously executing thread.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.167.195