Rust concurrency

Lessons learned from real life

Johan Burell, Valtech

About me

  • Worked as a developer 17y
  • Originally from C++, but mostly Java and some Kotlin/F# lately
  • Love ML-languages, and FP in general
  • Developer at Valtech, involved in innovations etc

Concurrent vs Parallel

Concurrency

Parallellism

Modularity

Responsiveness

Simultaneity

Throughput

Performance

Divide and conquer

About the

project

  • Interactive product display
  • Built for store-in-store use
  • Online purchase
  • Low manufacturing cost
  • Sensors, like camera, touch and pick-up detection

VISOR: VIrtualShOwRoom

Technology used

  • OpenGL                               (UI and product rendering, glium)
  • Intel RealSense Camera   (FFI-API, C/proprietary)
  • OpenCV                               (face recognition, CascadeClassifier)
  • Linux libevdev-api             (touch, /dev/input/[XXXXX])
  • Assimp                                (3D asset importer, mostly FBX-files)
  • rodio                                   (audio, backed by cpal/ALSA)
  • UDOO x86                          ($170 single board PC)

Face tracking

Interaction

Online shop

Checkout in mobile

Why Rust?

  • Lightweight
  • Portable
  • High performance
  • Good FFI-support
  • Great support for concurrency
  • Failsafe
  • Expression based(!)
  • ... and because I like it

Some drawbacks

  • No good FFI with C++
    • Possible, but I recommend a C-shim
  • Less mature ecosystem than Java/C++/.NET etc...
    • But good enough for most uses
  • Not the fastest language for rapid prototyping. (aka "Rust tax")
    • But wins in the long run (IMHO)
  • Only partial support for embedded development
    • But there is a WG and it is getting there

Concurrency in Rust

  • Threads                             (std::thread)
  • Lock-free/CAS-variables (std::sync::atomic)
  • Message passing             (std::sync::mpsc)
  • Callbacks                           (std::ops::{Fn, FnMut, FnOnce}
  • Futures                              (std::future::Future)
  • (async/await)                    (coming soon...)

Concurrency in Rust

  • Threads                             (std::thread)
  • Lock-free/CAS-variables (std::sync::atomic)
  • Message passing             (std::sync::mpsc)
  • Callbacks                           (std::ops::{Fn, FnMut, FnOnce}
  • Futures                              (std::future::Future)
  • (async/await)                    (coming soon...)

Types to help you out

Lifetime management:           Arc, lazy_static

Mutability:                                RefCell, Mutex/RwLock, Atomic

Traits for concurrency:           Send, Sync, Clone (Copy)

Channel types:                         Sender, Reciever

Atomics

Threading

Application

OpenGL

OpenCV

Evdev

RealSense

Text

Threading

Threading, cont...

Message passing

Application

glium events

evdev events

app events

handler

Message types

Channel setup

Receive messages

Polling events

Process domain events

Conclusion

  • There are a number of methods, all with their own benefits and drawbacks
  • You often get the best result by picking more than one method
  • Rust is awesome for concurrency and the promises holds true regarding security!
  • Futures and async will make a lot of boiler plate go away

The good parts

Conclusion

  • It does not protect you against deadlocking/livelocking and other logic bugs
    • But it is easier to track!
  • Safety makes the code verbose:
    • Arc::new(Mutex::new(None))
  • Simple things in unsafe languages can be frustratingly hard to model in Rust.
    • For example a mutable reference to an element in a mutable list.

The ugly parts

Thank you!

Made with Slides.com