#rw-lock #mutex #locking #debugging #padlock

padlock

Safely acquire RwLock/Mutex locks

3 unstable releases

0.2.0 May 4, 2020
0.1.1-alpha Feb 1, 2020
0.1.0-alpha Jan 28, 2020

#1307 in Concurrency

Download history 160/week @ 2024-12-25 394/week @ 2025-01-01 475/week @ 2025-01-08 654/week @ 2025-01-15 569/week @ 2025-01-22 589/week @ 2025-01-29 554/week @ 2025-02-05 592/week @ 2025-02-12 431/week @ 2025-02-19 545/week @ 2025-02-26 454/week @ 2025-03-05 760/week @ 2025-03-12 679/week @ 2025-03-19 604/week @ 2025-03-26 398/week @ 2025-04-02 441/week @ 2025-04-09

2,247 downloads per month
Used in 8 crates (via tray-item)

MIT/Apache

9KB
96 lines

Padlock

Safely acquire RwLock/Mutex locks and never worry about remembering to drop them!

CircleCI docs

License

MIT or Apache-2.0


lib.rs:

Aquire Mutex and RwLocks safely.

All methods in this crate will try to lock the passed Mutex or RwLock, if the locking fails, spin_loop_hint is called and we try again. This practice is called spinlock.

This means that all calls will block the current thread.

Important: When using methods like mutex_lock, remember that the lock is droped first when the lambda finishes running.

Example:

 use std::{
     thread,
     sync::{Arc, Mutex},
     time::Duration
 };

 #[derive(Debug)]
 struct Person {
     age: u8,
     name: String
 }

 fn main() {

     let people = Arc::new(Mutex::new(Vec::<Person>::new()));
     let mut threads = Vec::<thread::JoinHandle<()>>::new();

     // Write in one thread
     let people_clone = Arc::clone(&people);
     threads.push(thread::spawn(move || {

         for i in 0..10 {

             padlock::mutex_lock(&people_clone, |lock| {

                 lock.push(Person {
                     age: i * 10,
                     name: format!("Name {}", i)
                 });

             });

             thread::sleep(Duration::from_millis(500));

         }

     }));

     // Read from another
     let people_clone = Arc::clone(&people);
     threads.push(thread::spawn(move || {

         for _ in 0..6 {

             padlock::mutex_lock(&people_clone, |lock| {

                 for person in lock {
                     println!("{:?}", person);
                 }

             });

             thread::sleep(Duration::from_secs(1));

         }

     }));

     for t in threads {
         t.join();
     }

 }

No runtime deps