ReadWriteLock
ReadWriteLock is a synchronization primitive that allows multiple threads to acquire a shared lock for reading while ensuring that write operations have exclusive access. It supports a separation of concerns between readers and writers by providing two distinct locks: one for reading and one for writing. The lock is typically used to protect a shared resource with high-read, low-write access patterns.
If no thread holds the write lock, any number of reader threads can hold the read lock
In common programming libraries, the ReadWriteLock interface provides access to a readLock and a writeLock. The
Java's ReentrantReadWriteLock is a widely used example; C++ provides shared_mutex; pthreads provides pthread_rwlock_t. Depending on language
Read-write locks are appropriate when reads greatly outnumber writes and read operations can safely proceed concurrently.