39 lines
979 B
Rust
39 lines
979 B
Rust
// use crossbeam::atomic::AtomicCell;
|
|
use crossbeam_channel::unbounded;
|
|
use parking_lot::FairMutex;
|
|
use parking_lot::RwLock;
|
|
use std::sync::Arc;
|
|
use std::thread;
|
|
use std::time::Duration;
|
|
|
|
fn main() {
|
|
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 thead_mutex = mutex.clone();
|
|
let thread_rw_lock = rw_lock.clone();
|
|
thread::spawn(move || {
|
|
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();
|
|
});
|
|
|
|
loop {
|
|
let x = r.recv().unwrap();
|
|
if x < 0 {
|
|
break;
|
|
}
|
|
println!("Received: {}, mutext: {}, rw_lock: {}", x, *mutex.lock(), *rw_lock.read());
|
|
}
|
|
}
|