parking_lot
Efficient synchronization primitives for Rust with smaller, faster Mutex and RwLock than the standard library.
Official Documentation ↗parking_lot 0.12 / Rust 1.63+. Check the official docs for the latest API surface.Installation
cargo add parking_lotMethods
No methods match this filter.
Overview
parking_lot provides drop-in synchronization primitives that are smaller and faster than the standard library, with non-poisoning locks and efficient parking behavior.
Core Methods
Mutex and RwLock mirror the standard types with faster implementations, Condvar coordinates waiting threads, and Once enables one-time initialization. Guards deref to the protected value.
Usage Notes
Swap in parking_lot when lock overhead shows in profiles. Keep critical sections short and never hold a blocking lock across an await in async code.
Common Pitfalls
No poisoning means a panic while holding a lock is not reported, which can hide bugs. Blocking locks held across await points stall the executor. Over-locking turns contention into a bottleneck.
FAQ
- Why is parking_lot faster?
- It uses a smaller, more efficient lock representation and adaptive spinning before parking the thread, reducing overhead and improving cache behavior versus std locks.
- Why does lock not return a Result?
- parking_lot locks do not implement poisoning, so lock returns the guard directly. This removes error handling but also removes the safety net that flags a panic while holding the lock.
- Can I use it with async?
- The locks are blocking and should not be held across await points. Use an async-aware lock such as tokio::sync::Mutex when you must hold across awaits.
Report a problem with this API docOpens a pre-filled issue on GitHub.