take_lock 0.1.11

a dead simple lock around Option<Box<T>> thats similar in spirit to OnceLock but adds a bit more flexibility by paying a bit in performance
Documentation
  • Coverage
  • 87.5%
    7 out of 8 items documented6 out of 7 items with examples
  • Size
  • Source code size: 24.32 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 327.9 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 9s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • nevakrien/take_lock
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • nevakrien

take_lock

TakeLock is a minimal, thread-safe container for passing ownership of a single Box<T> between threads.

It provides an atomic, lock-free way to put, take, or swap a value across threads. This is implemented with a fairly straight forward use of atomics.

Example

use take_lock::TakeLock;
use std::thread;

let lock = std::sync::Arc::new(TakeLock::default());

let sender = {
    let lock = lock.clone();
    thread::spawn(move || {
        lock.put(Some(Box::new(123))).unwrap();
    })
};

let receiver = {
    let lock = lock.clone();
    thread::spawn(move || {
        loop {
            if let Some(val) = lock.take() {
                assert_eq!(*val, 123);
                break;
            }
        }
    })
};

sender.join().unwrap();
receiver.join().unwrap();