feat: parking_lot
This commit is contained in:
@@ -1,16 +1,38 @@
|
|||||||
|
// use crossbeam::atomic::AtomicCell;
|
||||||
use crossbeam_channel::unbounded;
|
use crossbeam_channel::unbounded;
|
||||||
// use lock_api::Mutex;
|
use parking_lot::FairMutex;
|
||||||
// use std::sync::Arc;
|
use parking_lot::RwLock;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// let lock = Arc::new(Mutex::new::<RawSpinlock, i32>(0_i32));
|
let mutex = Arc::new(FairMutex::new(0_i32));
|
||||||
|
let rw_lock = Arc::new(RwLock::new(0_i32));
|
||||||
|
// let cell = AtomicCell::new(0_i32);
|
||||||
|
|
||||||
let (s, r) = unbounded::<i32>();
|
let (s, r) = unbounded::<i32>();
|
||||||
|
|
||||||
|
let thead_mutex = mutex.clone();
|
||||||
|
let thread_rw_lock = rw_lock.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
s.send(1).unwrap();
|
for i in 1..=10 {
|
||||||
|
thread::sleep(Duration::from_millis(500));
|
||||||
|
s.send(i).unwrap();
|
||||||
|
|
||||||
|
let mut m = thead_mutex.lock();
|
||||||
|
*m += 1;
|
||||||
|
let mut rw = thread_rw_lock.write();
|
||||||
|
*rw += 1;
|
||||||
|
}
|
||||||
|
s.send(-1).unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
let x = r.recv().unwrap();
|
loop {
|
||||||
println!("Received: {}", x);
|
let x = r.recv().unwrap();
|
||||||
|
if x < 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
println!("Received: {}, mutext: {}, rw_lock: {}", x, *mutex.lock(), *rw_lock.read());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user