Commit 977e8493 authored by Hai Dang's avatar Hai Dang
Browse files

some cleanup

parent bbdc7000
Pipeline #42402 passed with stage
in 26 minutes and 56 seconds
......@@ -3,12 +3,10 @@ use ghostcell::GhostToken;
use std::sync::RwLock;
use std::{fmt, thread, time};
fn print_list<'id, T: fmt::Debug>(list: &NodePtr<'id, T>, token: &GhostToken<'id>) {
print!("[");
fn print_list<'id, T: fmt::Debug>(tag: char, list: &NodePtr<'id, T>, token: &GhostToken<'id>) {
for d in Node::iter(list, token) {
print!("{:?}, ", d);
print!("{}{:?}, ", tag, d);
}
print!("]");
}
fn main() {
......@@ -30,7 +28,7 @@ fn main() {
// Print the list we created
print!("Numbers: ");
// This only needs a &GhostToken
print_list(&list, &token);
print_list(' ', &list, &token);
println!();
// Oh, let's print it in parallel with thread `a` and thread `b`.
......@@ -39,12 +37,8 @@ fn main() {
// `b`'s can interleave.
println!("Parallel printing: ");
rayon::join(
|| for d in Node::iter(&list, &token) {
print!("a{:?} ", d);
},
|| for d in Node::iter(&list, &token) {
print!("b{:?} ", d);
}
|| print_list('a', &list, &token),
|| print_list('b', &list, &token),
);
println!();
......@@ -59,7 +53,7 @@ fn main() {
});
print!("Post deletion: ");
print_list(&list, &token);
print_list(' ', &list, &token);
println!();
// RwLock on the token to allow concurrent writes and reads.
......@@ -72,15 +66,11 @@ fn main() {
rayon::join(
|| {
let token : &GhostToken = &token.read().unwrap();
for d in Node::iter(&list, token) {
print!("a{:?} ", d);
}
print_list('a', &list, &token);
},
|| {
let token : &GhostToken = &token.read().unwrap();
for d in Node::iter(&list, token) {
print!("b{:?} ", d);
}
print_list('b', &list, &token);
}
);
println!();
......@@ -94,9 +84,7 @@ fn main() {
// Sleeps to allow the writer thread to acquire a writer lock.
thread::sleep(time::Duration::from_micros(10));
let token : &GhostToken = &token.read().unwrap();
for d in Node::iter(&list, token) {
print!("r{:?} ", d);
}
print_list('r', &list, token);
},
|| {
let token : &mut GhostToken = &mut token.write().unwrap();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment