Skip to content

Instantly share code, notes, and snippets.

@icezyclon
icezyclon / read_write_lock.py
Last active July 31, 2024 11:28 — forked from Eboubaker/read_write_lock.py
Python Reentrant Read Write Lock: Allowing Multithreaded Read Access But Only a Single Writer. Fixed a bug (deadlock while using read and write with context managers), updated with 3.10+ type information and added some tests.
import contextlib
import threading
from typing import Generator
class ReentrantRWLock:
"""This class implements reentrant read-write lock objects.
A read-write lock can be aquired in read mode or in write mode or both.
Many different readers are allowed while no thread holds the write lock.