/// make a ref-circle, it's possible in rust. /// but rust make sure it safe. use std::rc::Rc; use std::cell::RefCell; use crate::List::{Cons, Nil}; #[derive(Debug)] enum List{ Cons(i32, RefCell<Rc<List>>), Nil } impl List { fn tail(&self) -> Option<&RefCell<Rc<List>>> { match self { Cons(_, item) => Some(item), Nil => None, } } } fn main() { let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil)))); println!("a initial rc count = {}", Rc::strong_count(&a)); println!("a next item = {:?}", a.tail()); let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a)))); println!
Nov 1, 2020
2 min read
concurrent-programming: programs can run in independent with each other. parallel programming: program can run at same time.
as a low-level language, rust need less abstraction and more control.
use thread process vs thread
programs:
Race conditions: data-race. resource-race. DeakLock difficult concurrent bug. os-thread vs green-thread: 1:1 vs M:N
/// thread examples /// when the main-thread exit, the fork-thread will exit too. /// just like python thread.deamon flag. use std::thread; use std::time::Duration; fn main() { thread::spawn(|| { for i in 1.
concurrent-programming: programs can run in independent with each other. parallel programming: program can run at same time.
as a low-level language, rust need less abstraction and more control.
use thread process vs thread
programs:
Race conditions: data-race. resource-race. DeakLock difficult concurrent bug. os-thread vs green-thread: 1:1 vs M:N
/// thread examples /// when the main-thread exit, the fork-thread will exit too. /// just like python thread.deamon flag. use std::thread; use std::time::Duration; fn main() { thread::spawn(|| { for i in 1.
Nov 4, 2020
4 min read