A "slow write/fast read" lock could be used to lock memory mapped files that need to be exclusive locked sometimes for resizing.
The lock works as follows:
- All threads have a flag indicating if the have a read lock.
- There is a global write lock flag which is protected by a mutex with a condition.
Reader thread:
- Set the read flag belonging to the caller thread
- Check the write flag, if set
* Unset read flag
* Lock mutex and wait for condition to be signaled.
* Unlock mutex
* On signal retry.
- Do read operations
- Unset read flag
Writer thread
- Obtain mutex
- Set the write flag
- Wait for all threads read flags to be unset
(this requires going through all threads once)
- Do write operation
- Unset write flag
- Signal mutex
- Unlock mutex