Amanieu / parking_lot

Compact and efficient synchronization primitives for Rust. Also provides an API for creating custom synchronization primitives.
Apache License 2.0
2.76k stars 217 forks source link

How to fail to acquire write lock if read lock already acquired? #436

Open shivam-880 opened 7 months ago

shivam-880 commented 7 months ago

Is there a way for acquiring writer lock to fail if there is a reader lock already acquired but not if there is a write lock? What we really want is the ability to fail to acquire the write lock if read lock is already acquired instead of waiting for it!

Amanieu commented 7 months ago

That's not possible: RwLock only tracks the number of readers so it doesn't know whether the current thread holds a read lock or not.

shivam-880 commented 7 months ago

No, what I meant was to fail acquiring write lock by any thread if any of the threads have a read lock ie., if read lock count > 1