External Publication
Visit Post

Idea : Lock Free STD Collection

Rust Internals [Unofficial] March 5, 2026
Source

Idea : A lock-free version of the std collection.

Motivation :

  • Lock free performance is generally superior to mutex/rwlock based synchronization.
  • The code becomes cleaner because there is no need to manually invoke mutexes or locks, one simply uses std::collection::{LockfreeVec, LockfreeMap} and Arc::new(LockfreeVec::new()).
  • The user is safe from deadlock "footguns."

Reason for Inclusion in std :

  • To ensure ecosystem interoperability. If implemented in third party libraries, Library A might use Lock-free Library A, while Library B uses Lock-free Library B. If I use both libraries and need to pass the output of Library A (returning Lock-free Type A) to Library B (expecting Lock-free Type B), they become incompatible.

Benefits : It offers the same interoperability benefits as the current Vector implementation, where all users are compatible with one another. The usage is also significantly simpler than Arc<Mutex<Vec>> because it eliminates the need for locking, which can cause deadlocks while this approach is deadlock free.

For example :

let a = Arc::new(LockfreeVec::with_capacity(10));
let b = a.clone();

thread::spawn(move || {
    b.push("input");
}).join().unwrap();

Discussion in the ATmosphere

Loading comments...