Python RLock Class | release() Method with Example

Python RLock.release() Method: Here, we are going to learn about the release() method of RLock Class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 18, 2020

Python RLock.release() Method

release() is an inbuilt method of the RLock class of the threading module in Python.

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:

    from threading import RLock

Syntax:

    release()

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:

# 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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.