Python RLock release() Method with Example

Python RLock.release() Method: In this tutorial, we will learn about the release() method of RLock Class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 25, 2023

Python RLock.release() Method

The RLock.release() is an inbuilt method of the RLock class of the threading module. The RLock class objects follow reentrancy. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; and the thread must release it once for each time it has acquired it. This method releases the lock, thereby decrementing the recursion level. If after the decreasing it becomes zero, then the lock is set to unlock, and if any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed. If after the decreasing the recursion level is still nonzero, the lock remains locked and still owned by the calling thread.

Module

The following module is required to use release() method:

import threading

Class

The following class is required to use release() method:

from threading import RLock

Syntax

The following is the syntax of release() method:

release()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'NoneType'>. The method does not return anything. It releases the thread which had acquired it and decreasing the recursion level of the locked thread. If the level becomes zero, it is unlocked by the thread, and if the level is still nonzero, it is owned by the calling thread only.

Example of RLock.release() Method in Python

# program to illustrate the use of 
# release() method in RLock class
  
# importing the module 
import threading 
  
# initializing the shared resource 
x = 0
  
# creating an RLock object  
rlock = threading.RLock() 
  
# Creating first thread
rlock.acquire() 
x = x + 1
  
# the below thread is trying to access  
# the shared resource  
rlock.acquire()  
x = x + 2
rlock.release()

rlock.release() 
# Rlock released by both the threads
  
# displaying the value of shared resource 
print("Displaying the final value of the shared resource x:", x)

Output

Displaying the final value of the shared resource x: 3




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.