
Moritz escribió:
Hi everybody,
I have a question concerning the interprocess::updgradable::mutex.
My scenario:
The idea is that the writer locks the upgradable_mutex normally (calling lock()) and the readers lock it sharable (calling lock_sharable()). If the mutex is locked sharable and the writer tries to lock it normally, another attempt to lock it sharable has to wait until the writer succeeded in locking and unlocking the mutex.
An upgradable mutex is efficient if the writer not always writes, but it also reads many times. In that case, acquiring upgradable lock still allows concurrent readers that have acquired sharable locks. The reader-writer can read but can also write *if upgrades the upgradable lock to an exclusive lock*. If the reader-writer realizes that a write is necessary it should call unlock_upgradable_and_lock() which blocks until all readers finish. There is no guarantee that new readers will not get sharable lock while the upgradable lock calls unlock_upgradable_and_lock(), because all of them need to grab an internal mutex and this scheduling is done by the OS. You can only guarantee that new readers will block *after* unlock_upgradable_and_lock() has been completed. Best, Ion