Acquires a lock meant for both reading and writing.
Acquires a lock meant for both reading and writing.
Acquires a lock meant for reading.
Acquires a lock meant for reading. Multiple threads can acquire the lock at the same time. It is also re-entrant (i.e. the same thread can acquire it multiple times)
Acquires a lock meant for writing.
Acquires a lock meant for writing. Only one thread can acquire the write lock at the same time and it has to wait until all active reads are finished.
Naive read-write lock, meant for low-contention scenarios.
Not a good idea to use it in high-contention scenarios, as the locking is unfair (i.e. writes have priority over reads, but otherwise it provides no guarantees to the fairness of what thread gets the lock next), plus spinlocking means that threads never wait and thus consume CPU resources.
Example: