Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust - A walk to concurrency

flaper87
February 23, 2015

Rust - A walk to concurrency

A walk through Rust internals and type-system to a concurrent Rust application.

flaper87

February 23, 2015
Tweet

More Decks by flaper87

Other Decks in Programming

Transcript

  1. Rust - A walk to
    concurrency

    View Slide

  2. View Slide

  3. For attending
    Still here
    feel free to interrupt
    @flaper87
    [email protected]

    View Slide

  4. What’s up with this Rust-lang
    thingy ?

    View Slide

  5. Does it really buy me
    something?

    View Slide

  6. Boring Stuff
    ~ and @
    sigils are
    gone

    View Slide

  7. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default

    View Slide

  8. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii

    View Slide

  9. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii
    almost
    everything
    is an
    expression

    View Slide

  10. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii
    almost
    everything
    is an
    expression
    static with
    local
    inference

    View Slide

  11. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii
    almost
    everything
    is an
    expression
    static with
    local
    inference
    ...

    View Slide

  12. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii
    almost
    everything
    is an
    expression
    static with
    local
    inference
    ...
    ...

    View Slide

  13. Boring Stuff
    ~ and @
    sigils are
    gone
    Immutable
    by default
    raii
    almost
    everything
    is an
    expression
    static with
    local
    inference
    ...
    ...
    ...

    View Slide

  14. Memory Safety

    View Slide

  15. passing by-ref “lends” the value
    fn main() {
    let mut x = 5;
    add_one(&mut x);
    println!("{}", x);
    }
    fn add_one(num: &mut i32) {
    *num += 1;
    }

    View Slide

  16. fn main() {
    let x = Box::new(5);
    add_one(x);
    println!("{}", x); // error
    }
    fn add_one(mut num: Box) {
    *num += 1;
    }
    passing by-val “gives” the value

    View Slide

  17. Lifetimes

    View Slide

  18. struct Foo<'a> {
    x: &'a i32,
    }
    fn main() {
    let x; // -+ x goes into scope
    // |
    { // |
    let y = &5; // ---+ y goes into scope
    let f = Foo { x: y }; // ---+ f goes into scope
    x = &f.x; // | | error here
    } // ---+ f and y go out of scope
    // |
    println!("{}", x); // |
    }
    lifetimes, regions, scope…

    View Slide

  19. Generics

    View Slide

  20. struct MyType {...}

    View Slide

  21. Data-race
    Safety

    View Slide

  22. Bounds
    T: Send
    T: Sync
    T: Sync+Copy
    T: U

    View Slide

  23. No dead-lock protection

    View Slide

  24. Thread, Mutex,
    Channel

    View Slide

  25. ARC use std::sync::{Arc, Mutex};
    use std::thread;
    use std::sync::mpsc;
    fn main() {
    let data = Arc::new(Mutex::new(0u32));
    let (tx, rx) = mpsc::channel();
    for _ in 0..10 {
    let (data, tx) = (data.clone(), tx.clone());
    thread::spawn(move || {
    let mut data = data.lock().unwrap();
    *data += 1;
    tx.send(());
    });
    }
    for _ in 0..10 {
    rx.recv();
    }
    }
    Atomic
    Reference
    Counted

    View Slide

  26. Mutex use std::sync::{Arc, Mutex};
    use std::thread;
    use std::sync::mpsc;
    fn main() {
    let data = Arc::new(Mutex::new(0u32));
    let (tx, rx) = mpsc::channel();
    for _ in 0..10 {
    let (data, tx) = (data.clone(), tx.clone());
    thread::spawn(move || {
    let mut data = data.lock().unwrap();
    *data += 1;
    tx.send(());
    });
    }
    for _ in 0..10 {
    rx.recv();
    }
    }
    Scoped Guards
    Poisoned

    View Slide

  27. Channels use std::sync::{Arc, Mutex};
    use std::thread;
    use std::sync::mpsc;
    fn main() {
    let data = Arc::new(Mutex::new(0u32));
    let (tx, rx) = mpsc::channel();
    for _ in 0..10 {
    let (data, tx) = (data.clone(), tx.clone());
    thread::spawn(move || {
    let mut data = data.lock().unwrap();
    *data += 1;
    tx.send(());
    });
    }
    for _ in 0..10 {
    rx.recv();
    }
    }
    Multi-producer
    Single-consumer

    View Slide

  28. Questions?

    View Slide