About
A shared lock is a lock acquired in the shared mode over a data structure.
The data structure can be any:
- from a file
A shared lock:
- allows two or more process/thread to read from the data structure at the same time.
- prevents another process from writing to the data structure while a process is reading it
Shared locks are usually used while the process is reading data (SQL SELECT operation , …)
A process/thread gets ownership of the data structure by obtaining the shared lock.
This is the first step step toward reading a file that can be accessed concurrently. For instance, a database application will acquire a shared lock on its database file before reading it.
Why ?
Without this lock, if a process is writing in the data structure (file, map, collection, …), a reading process might read:
- some data before the change
- and other data after the change.
The read would then not be consistent and the write change will then appear not atomic (even if it's).
In the context of a transaction, this read are called dirty read.