parking_lot for Rust: Fast Synchronization Primitives
\n\nThe parking_lot library is one of the most popular and performant implementations of synchronization primitives for the Rust language. It provides an alternative to standard mutexes (std::sync::Mutex), read-write locks (std::sync::RwLock), and condition variables (std::sync::Condvar), but with significantly better performance and smaller code size.
Why is this library needed? Standard synchronization primitives in Rust, while safe, have several drawbacks: they use system calls (futex on Linux) even in simple scenarios, require additional memory for flags, and have a more complex internal implementation. parking_lot solves these problems by using more efficient algorithms (e.g., optimistic locks and spin-loops) and minimizing overhead. As a result, code runs faster, especially in multi-threaded applications with high contention.
\n\nThe library is fully compatible with Rust and does not require unsafe code on the user's side. It offers the same interfaces as the standard library, but with additional features: Mutex with timeout support, RwLock with a fair queue, Once for lazy initialization, and ReentrantMutex for reentrant locks. parking_lot is the de facto standard for high-performance Rust applications, from web servers to game engines.
Installation
\n\nAdd the library to your project's Cargo.toml file:
[dependencies]\nparking_lot = "0.12"\n\nThen run the command:
\n\ncargo build\n\nOr, if you use cargo add (starting from Rust 1.62):
cargo add parking_lot\n\nAfter that, you can import primitives from the library:
\n\nuse parking_lot::{Mutex, RwLock, Condvar, Once, ReentrantMutex};\n\nQuick Start: Minimal Working Example
\n\nHere is a simple example of using Mutex from parking_lot to protect a shared counter in a multi-threaded environment:
use parking_lot::Mutex;\nuse std::thread;\n\nfn main() {\n // Create a mutex wrapping the value\n let counter = Mutex::new(0);\n\n // Create 10 threads, each increments the counter by 1\n let mut handles = vec![];\n for _ in 0..10 {\n let handle = thread::spawn(|| {\n // Acquire the lock — .lock() returns an RAII guard\n let mut guard = counter.lock();\n *guard += 1;\n // guard is automatically released when it goes out of scope\n });\n handles.push(handle);\n }\n\n // Wait for all threads to finish\n for handle in handles {\n handle.join().unwrap();\n }\n\n // Read the final value\n let final_value = *counter.lock();\n println!("Final value: {}", final_value); // Should be 10\n}\n\nIn this example, counter.lock() returns a MutexGuard, which automatically releases the lock upon destruction. Unlike the standard std::sync::Mutex, there is no need to handle errors (poisoning) — parking_lot does not poison the mutex on panic by default.
Main Methods and Types
\n\nBelow are 8 key methods/types of the parking_lot library with signatures, descriptions, and examples.
\n\n1. Mutex::lock()
\nSignature: pub fn lock(&self) -> MutexGuard<'_, T>
Description: Acquires the mutex, blocking the current thread until the lock is obtained. Returns an RAII guard that automatically releases the lock upon exit
Also in library
doctest for C++: Complete Guide with Examples
HTTP Client for C# (.NET): Top Libraries for Working with Web Requests
Generating PDF Reports in Rust: The printpdf Library