Skip to content
0:00 / 0:00

#rust#atomics#memoryordering

Crust of Rust: Atomics and Memory Ordering

Jon Gjengset

111K subscribers • 38 videos on Hallway

YouTube
  • Recommended by 7 people: 1 on Lobsters and 6 in engineering communities.
  • A classic. People kept recommending it from 2021 to 2024.
  • Presented by Jon Gjengset

Advanced. Deep dive for people who know the field.

109K views5 years ago7 recommendations

Systems programmingProgramming languagesRustConcurrency and parallelismMemory management

Jon Gjengset explains Rust's atomic types and memory ordering, demonstrating how to implement concurrency primitives like mutexes.

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. Introduction What are atomics? The Memory Model AtomicUsize Questions so far Implementing a (bad) Mutex Our Mutex works! Pesky thread interleavings compare_exchange Mitigating compare_exchange contention compare_exchange_weak Ordering::Relaxed Ordering::Acquire/Release The fetch_ methods Ordering::SeqCst Breather ThreadSanitizer loom Atomic fences volatile AtomicPtr Atomics through FFI Consume ordering? Closing thoughts You can read more about Rust's memory model in the Rust reference at doc.rust-lang.org/nightly/reference/memory-model.html and in the C++ memory model documentation at en.cppreference.com/w/cpp/atomic/memory_order. Live version with chat: youtube.com/watch?v=0Vq2aJxwB1o

Why it's here

7 people recommend this

What people say. Recommended as an in-depth explanation of Rust atomics and memory ordering.

u/fluzz142857in r/rust • Recommends

On “If I use one shared AtomicBool as an indicator, do I need to worry about the ordering?”

Here is Jon Gjengset’s video on atomics: [video I have not watched it myself but I have watched his other videos in the same format and style, and I can vouch for those.

u/masklinnin r/rust • Recommends

On “Miri now produces relaxed memory effects”

Proper use of stronger memory orderings. /u/jonhoo has an excellent crust of rust on the subject: link to this video Though normally that’s only a concern if you’re trying to write your own concurrency primitives. If you use higher-level ones (mutex, rwlock, …) and use atomics as atomics not as building blocks it’s not an issue you’ll have to face. For an appetizer (a shorter dive than Jonhoo’s video) you can also read https://preshing.com/20121019/this-is-why-they-call-it-a-weakly-ordered-cpu/ I actually translated it to Rust0 a while back (though apparently never uploaded that anywhere). The rust compiler is rather unhappy with mutable globals so it’s actually harder work to get to the wrong beh…

itamarston Lobsters • Recommends

Atomics and Memory Ordering (demonstrated in Rust)

u/WrongJudgment6in r/rust • Recommends

On “Question: Find the right ordering for Atomics”

While I can't really answer that, I can suggest an in depth stream about atomics link to this video

u/kam821in r/rust • Recommends

On “What is the use of AtomicBool?”

Here is fantastic Jon Gjengset's episode explaining Atomic and Memory ordering. link to this video

u/seftontychoin r/rust • Recommends

On “Rust has exposed my lack of knowledge on how computers work.”

You might also enjoy: link to this video

u/Top-Ant493in r/rust • Recommends

On “My (few) reservations about Rust”

> Are there any resources to learn how to do parallel programming in rust without Tokio and other third party implementations ? Rust Atomics and Locks by Mara Bos is a great book to learn more about safe concurrency. Here's also a link to a video on Rust atomics and memory safety by Jon Gjengset: Crust of Rust: Atomics and Memory Ordering This is what I'm currently learning to understand concurrency/parallel programming in Rust.

u/DzenanJupicin r/rust • Mentions

On “Hey Rustaceans! Got an easy question? Ask here (29/2021)!”

If you're curious about the implementation I'd generally recommend having a look at the implementation of (in this case) `Rc`. ## TL;DR The essence is, that `Rc` uses interior mutability for the counter. Interior mutability means you have an immutable reference but are able to mutate the underlying type. You can also see this in i.e. `Mutex`. ## More insights `Rc` in particular uses `Cell` for the counter. `Cell` is the only safe way in Rust to mutate an immutable type. It internally uses `UnsafeCell`, which is the only sound way to mutate an immutable type, since the compiler guarantees to not apply certain optimizations that would otherwise break your code if you mutate immutable state…

u/stdusrin r/rust • Mentions

On “Sharing counters among threads”

If you have the time: link to this video

u/hardfau18in r/rust • Mentions

On “Is int and bool are thread safe?”

What could go wrong, i mean i don't know what are the possibility of undefined behaviour, Here in one case they are using if some data is available in one buffer, then once the buffer is written that flag is set, is it possible that even before the flag is set, another thread can see that the flag is set? That would be dangerous. It sounds like magic but it says like that in this video link to this video at 1:46:00

u/__maccas__in r/rust • Mentions

On “When you learned rust did you take notes or swallowed the whole book at once?”

That's an excellent addition. I'm having that too. I initially learnt all my atomics stuff from Jon G on YT