DEV Community

Hitesh Chawla
Hitesh Chawla

Posted on

R-Lock vs Lock in Python

In Python, both Lock and RLock are synchronization primitives provided by the threading module to manage access to shared resources in multithreaded programs. However, they differ in their behavior and use cases.


1. Lock (threading.Lock)

  • Description: A Lock is a simple locking mechanism that allows only one thread to acquire the lock at a time. Once a thread acquires the lock, any other thread trying to acquire it will block until the lock is released.
  • Non-reentrant: A thread that already holds the lock cannot acquire it again until it releases it. If it attempts to do so, it will cause a deadlock.
  • Use Case: Use Lock when a thread only needs to acquire the lock once and release it after finishing its work.

Example of Lock:

import threading

lock = threading.Lock()

def critical_section():
    lock.acquire()
    try:
        print(f"{threading.current_thread().name} is in the critical section")
    finally:
        lock.release()

thread1 = threading.Thread(target=critical_section)
thread2 = threading.Thread(target=critical_section)

thread1.start()
thread2.start()
thread1.join()
thread2.join()
Enter fullscreen mode Exit fullscreen mode

2. RLock (threading.RLock)

  • Description: A Reentrant Lock (RLock) is a more sophisticated lock that allows the same thread to acquire the lock multiple times without causing a deadlock. Each acquire must be paired with a corresponding release.
  • Reentrant: A thread can re-acquire the lock it already holds as long as it releases it the same number of times.
  • Use Case: Use RLock when a thread may need to acquire the same lock multiple times, for example, in recursive functions or when one lock-protected operation calls another lock-protected operation.

Example of RLock:

import threading

rlock = threading.RLock()

def recursive_function(count):
    rlock.acquire()
    try:
        print(f"{threading.current_thread().name} acquired the lock: count = {count}")
        if count > 0:
            recursive_function(count - 1)  # Recursive call acquires the lock again
    finally:
        rlock.release()

thread = threading.Thread(target=recursive_function, args=(3,))
thread.start()
thread.join()
Enter fullscreen mode Exit fullscreen mode

Key Differences Between Lock and RLock

Feature Lock (threading.Lock) RLock (threading.RLock)
Reentrancy Not reentrant (deadlock if re-acquired by the same thread). Reentrant (same thread can acquire multiple times).
Use Case Simple locking mechanisms without nested or recursive locking. Recursive locking or nested locking scenarios.
Performance Slightly faster and simpler. Slightly more overhead due to reentrancy support.

When to Use Which?

  • Use Lock for simpler cases where reentrancy is not required.
  • Use RLock if you anticipate scenarios where a thread may need to re-acquire the same lock, such as recursive function calls or nested lock usage.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay