Python difference lock and RLock object : Thread based parallelism














































Python difference lock and RLock object : Thread based parallelism



Difference between Lock and Rlock objects

A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an OS (Operating System). In simple words, a thread is a sequence of such instructions within a program that can be executed independently of other codes. For simplicity, you can assume that a thread is simply a subset of a process!


Locks

These are the simplest primitive for synchronization in Python. There are two states of a lock i.e locked and unlocked. A lock is a class in the threading module whose object generated in the unlocked state and has two primary methods i.e acquire() and release(). When the acquire() method is called, it locks the execution of the Lock and blocks its execution until the release() method is called in some other thread which sets it to unlock state. Locks help us in efficiently accessing a shared resource in a program in order to prevent corruption of data, it follows mutual exclusion as only one thread can access a particular resource at a time.

Let us look at the below example to understand the use of Locks:

# program to illustrate the use of Locks
  
  
import threading
  
  
# creating a Lock object
lock = threading.Lock()
  
# initializing the shared resource
cpp = 0
  
def sumOne():
       global cpp
     
       # locking the shared resource
       lock.acquire()
       cpp = cpp + 1
     
       # unlocking the shared resource
       lock.release()
  
def sumTwo():
       global cpp
     
       # locking the shared resource
       lock.acquire()
       cpp = cpp + 2
     
       # unlocking the shared resource 
       lock.release()
  
  
# calling the functions
sumOne()
sumTwo()
  
# displaying the value of shared resource
print(cpp)
Output:
3

In the above program, lock is a Lock object, the global variable cpp is a shared resource and sumOne() and sumTwo() functions act as threads, in sumOne() function the shared resource cpp is first locked and then incremented by 1 and then cpp is released and in sumTwo() function the variable cpp is first locked and then incremented by 2 and then cpp is released.The two functions sumOne() and sumTwo() can not access the shared resource cpp simultaneously, only one of them can access the shared resource cpp at a time.



RLocks


The default Lock doesn't recognize which thread the lock currently holds. If the shared resource is being accessed by any thread then other threads trying to access the shared resource will get blocked even if it is the same thread that locked the shared resource. The Re-entrant lock or RLock is used in these situations to prevent undesired blocking from accessing the shared resource. If a shared resource is in RLock then it can be called again safely. The RLocked resource can be accessed repeatedly by various threads, though it still works correctly when called by different threads.

Let us look at the below example to understand the use of RLocks:

import threading
# initializing the shared resource
cpp = 0
  
# creating a Lock object 
lock = threading.Lock()
  
# the below thread is accessing the
# shared resource
lock.acquire()
cpp= cpp + 1
  
# This thread will be blocked
lock.acquire() 
cpp = cpp + 2
lock.release()
  
# displaying the value of shared resource
print(cpp)

In the above program, two threads are trying to access the shared resource cpp simultaneously, here when a thread is currently accessing shared resource cpp the other thread will be prevented from accessing it. When two or more threads try to access the same resource effectively prevent each other from accessing the resource this is known as deadlock due to which the above program generates no output.

However, the above problem in the program can be solved by using RLock.

program to illustrate the use of RLocks
  
# importing the module
import threading
  
# initializing the shared resource
cpp = 0
  
# creating an RLock object instead 
# of Lock object 
lock = threading.RLock()
  
# the below thread is trying to access 
# the shared resource
lock.acquire()
cpp = cpp + 1
  
# the below thread is trying to access 
# the shared resource 
lock.acquire() 
cpp = cpp + 2
lock.release()
lock.release()
  
# displaying the value of shared resource
print(cpp)


Output:

3

Here, there is no unwanted prevention of accessing the shared resource geek by the threads in the program. We need to call release() once for each acquire() of RLock object lock.

From the numerous programs and explanations mentioned above there are many differences between a Lock object and an RLock object in Python:

LocksRLocks
A Lock object can not be acquired again by any thread unless it is released by the thread which which is accessing the shared resource.An RLock object can be acquired numerous times by any thread.
A Lock object can be released by any thread.An RLock object can only be released by the thread which acquired it.
A Lock object can be owned by noneAn RLock object can be owned by many threads
Execution of a Lock object is faster.Execution of an RLock object is slower than a Lock object
In next article , we will learn about Lock.acquire().



More Articles of Diksha Kumari:

Name Views Likes
Python : py_compile | Generate byte code 308 0
Python : py_compiler |Convert py to pyc 1157 0
Python : py_compile |Compile python sources file 287 0
Python :How to lock Critical Sections(Thread based parallelism) 249 0
Python : Locking without Deadlocks(Thread based Parallelism ) 180 0
Python : Communicating Between Threads-2(Thread based parallelism) 389 0
Python : Communicating Between Threads -1(Thread based parallelism) 176 0
Python Start and Stop a thread (Thread based parallelism) 165 0
Python: Check if a Thread has started (thread based parallelism) 144 0
Python Thread-based parallelism :main_thread() 229 0
Python Lock Class | acquire() Method(thread based parallelism) 294 0
Python Thread-based parallelism : get_native_id() 460 0
Python Thread-based parellelism: active_count() 269 1
Python Lock Class | lock() Method(thread based parallelism) 246 0
Python Thread-based parallelism : stack_size([size]) 331 1
Python Thread Local Data(thread based parallelism 265 1
Python Semaphore Objects (thread based parallelism) 264 0
Python Event class - set() method | Thread based parallelism 252 1
Python Condition object (thread based parallelism) 411 0
Python Thread based parallelism : Run() 228 0
Python Thread-based parallelism : current_thread() 254 1
Python Condition object - acquire()(thread based parallelism) 190 0
Python Thread-based parallelism : TIMEOUT_MAX () 423 0
Python Condition object - notify_all() ( thread based parallelism ) 192 0
Python Thread-based parallelism :.settrace(func) 308 0
Python difference lock and RLock object : Thread based parallelism 1083 0
Python Event class - clear() method | Thread based parallelism 188 0
Python Thread object -start() (thread based parallelism) 335 0
Python RLock Class - acquire() Method (thread based parallelism) 226 0
Python Thread-based parallelism : enumerate() 246 0
Python Thread based parallelism: get_ident() 302 1
Python Thread-based parallelism : setprofile(func) 215 0
Python RLock Class - release() Method (thread based parallelism) 194 0
Python Barrier object | Thread based parallelism 228 0
Python Condition object - notify () ( thread based parallelism ) 217 0
Python Event class - is_set() method | Thread based parallelism 173 0
Python Thread based parallelism : join() 317 0
Python thread based parallelism : set.name() 200 0
Python Event class - wait() method | Thread based parallelism 243 0
PYTHON Thread based parallism 293 1
Python Barrier method | Thread based parallelism 228 0
Python Condition object - release() ( thread based parallelism ) 184 0
Python Thread based parallelism: excepthook 1487 1
Python thread object : is_alive() (thread based parallelism) 996 0
Python Condition object - wait() ( thread based parallelism ) 215 0
Python Lock Class | release() Method(thread based parallelism) 254 0
Python Thread object (thread based parallelism) 259 0
Python Timer class - start() method | Thread based parallelism 175 1
Python thread based parallelism : get.name() 322 1
Python Timer class - cancel() method | Thread based parallelism 206 1
Python thread object : Deamon() (thread based parallelism) 241 0

Comments