Tokio has owned guards for
Arc<Mutex<T>> (and semaphores, etc.) that act like mutex guards but don't have lifetime parameters (i.e., are
: 'static if
T itself is). Why doesn't
- Is this an idiom that is particularly necessary for async code?
- Are owned guards easy to implement on top of
- Are they implemented in a widely-used crate?
- Are owned guards a bad idea in sync code?
parking_lot does have this, it's just behind a feature flag: parking_lot/mutex.rs at lock_api-0.4.9 · Amanieu/parking_lot · GitHub
The only thing it does is extend the lifetime on the guard to
'static. That makes it usable in more situations. I suppose the biggest downside might be that this makes it more trivial to "leak" the lock (e.g., storing it in a global cache without an invalidation policy) thus starving other waiters.
They are a bad idea in both sync and async code, but in async code you, often, don't have a choice because you want to keep lock while calling some function which-is-usually-very-quick-but-compiler-doesn't-know-that (and sometimes said function may not be quick at all).
If you really need them in sync code you can use
parking_lot, but most of the time it's bad idea.
Do you know of some code where owned locks are used?
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.