Crust of Rust: Atomics and Memory Ordering

Crust of Rust: Atomics and Memory Ordering

In this episode of Crust of Rust, we go over Rust's atomic types, including the mysterious Ordering enum. In particular, we explore the std::sync::atomic module, and look at how its components can be used to implement concurrency primitives like mutexes. We also investigate some of the gotchas and sometimes counter-intuitive behaviors of the atomic primitives with different memory orderings, as well as strategies for testing for and debugging errors in concurrent code.

In this episode of Crust of Rust, we go over Rust's atomic types, including the mysterious Ordering enum. In particular, we explore the std::sync::atomic module, and look at how its components can be used to implement concurrency primitives like mutexes. We also investigate some of the gotchas and sometimes counter-intuitive behaviors of the atomic primitives with different memory orderings, as well as strategies for testing for and debugging errors in concurrent code.

  • 0:00:00 Introduction
  • 0:02:03 What are atomics?
  • 0:05:26 The Memory Model
  • 0:07:33 AtomicUsize
  • 0:12:23 Questions so far
  • 0:20:20 Implementing a (bad) Mutex
  • 0:27:39 Our Mutex works!
  • 0:33:04 Pesky thread interleavings
  • 0:39:42 compare_exchange
  • 0:44:54 Mitigating compare_exchange contention
  • 0:50:43 compare_exchange_weak
  • 0:57:02 Ordering::Relaxed
  • 1:12:13 Ordering::Acquire/Release
  • 1:26:00 The fetch_ methods
  • 1:34:07 Ordering::SeqCst
  • 2:00:08 Breather
  • 2:00:40 ThreadSanitizer
  • 2:05:49 loom
  • 2:22:09 Atomic fences
  • 2:27:27 volatile
  • 2:32:18 AtomicPtr
  • 2:35:13 Atomics through FFI
  • 2:36:44 Consume ordering?
  • 2:38:08 Closing thoughts

rust

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Rust Casting, Shadowing, Consts and Static

A mix of topics of casting, shadowing, constants and static variables inside the Rust Programming Language. This Rust programming language tutorial series is aimed at easing your training step by step.

Rust Zürichsee, April: Gerhard Bräunlich - PyO3 and Rust in action

This presentation was the first experiment with livestreaming of the Rust Zürisee meetup group in Switzerland. Gerhard is sharing parts of his story migratin...

"Rust Isn’t Afraid to be Imperfect as Long as we Ship something Useful"

Steve Klabnik is a member of the Rust core team, an active open-source contributor, and author of The Rust Programming Language, Rails 4 in Action, and Designing Hypermedia APIs books. In 2012 and 2016, we invited Steve to speak at the RailsClub (now RubyRussia) conference. Since then, Steve has been working on Rust a lot, did a lot of interesting things and we realized that we should definitely interview him once again!

Prevent Breaking Code Changes in Future Releases using `non exhaustive` enums in Rust

Hello everyone, recently I have come across a feature in Rust, known as non_exhaustive. It was introduced in Rust 1.40.0 . This attribute prevents source code-breaking changes in projects downstream.

Rust vs Go - Which Is More Popular?

Rust vs Go - Which Is More Popular - Go and Rust are two of the hottest compiled programming languages. I develop in Go full-time and love it, and I'm learning more about Rust recently - its an exc