You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write-preferring RW locks avoid the problem of writer starvation by preventing any new readers from acquiring the lock if there is a writer queued and waiting for the lock
Fairness is ensured using a first-in, first-out queue for the tasks awaiting the lock; if a task that wishes to acquire the write lock is at the head of the queue, read locks will not be given out until the write lock has been released.
If I am not mistaken, the difference is that Wikipedia says that if there is a writer anywhere in the queue no new readers can acquire the lock, while the Tokio documentation seems to say that only when the writer is at the head of the queue no new readers can acquire the lock.
The text was updated successfully, but these errors were encountered:
I don't have a question. I think Tokio's documentation is confusing.
The documentation of Tokio's RwLock says it is write-preferring and links to a Wikipedia article explaining what that means.
However, Tokio's implementation seems to use a different policy than Wikipedia does.
This is fine, but it might be confusing to people because both are mentioned as being true for Tokio's RwLock at the same time.
I think the documentations is clearer when the link to Wikipedia is removed.
it just different variant of the lock
Are you talking about Readers-Writer locks in general? Or is there a way in Tokio to switch priority policies?
Tokio's rwlock is using a write-preferring fairness policy. Whenever a writer tries to acquire the lock, it's guaranteed to get it eventually. This happens at the expense of readers, since a reader may fail to acquire the lock despite the lock being in use by other readers.
I agree that RwLock is write-preferring and I'm not claiming that Tokio's documentation (or the code, for that matter) is wrong — I just noticed that the Tokio's definition of write-preferring is different from Wikipedia's definition.
Specifically, the Wikipedia version is unfair, whereas Tokio's is fair.
If users click the link in Tokio's documentation, believing that it is appropriate for RwLock, they may get the (false) impression that Tokio's lock is unfair.
The RwLock documentation links to this Wikipedia section: https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Priority_policies.
Which says:
However, the Tokio documentation says:
If I am not mistaken, the difference is that Wikipedia says that if there is a writer anywhere in the queue no new readers can acquire the lock, while the Tokio documentation seems to say that only when the writer is at the head of the queue no new readers can acquire the lock.
The text was updated successfully, but these errors were encountered: