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

Rust - A walk to concurrency

C05edcc8a57f64b4e040d94ad89cee57?s=47 flaper87
February 23, 2015

Rust - A walk to concurrency

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

C05edcc8a57f64b4e040d94ad89cee57?s=128

flaper87

February 23, 2015
Tweet

Transcript

  1. Rust - A walk to concurrency

  2. None
  3. For attending Still here feel free to interrupt @flaper87 flavio@redhat.com

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

  5. Does it really buy me something?

  6. Boring Stuff ~ and @ sigils are gone

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

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

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

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

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

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

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

    default raii almost everything is an expression static with local inference ... ... ...
  14. Memory Safety

  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; }
  16. fn main() { let x = Box::new(5); add_one(x); println!("{}", x);

    // error } fn add_one(mut num: Box<i32>) { *num += 1; } passing by-val “gives” the value
  17. Lifetimes

  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…
  19. Generics

  20. struct MyType<T, E> {...}

  21. Data-race Safety

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

  23. No dead-lock protection

  24. Thread, Mutex, Channel

  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
  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
  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
  28. Questions?