6

Crust of Rust: Atomics and Memory Ordering

 3 years ago
source link: https://www.youtube.com/watch?v=rMGWeSjctlY
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
0:00 / 2:39:19
LOS ANGELES

Crust of Rust: Atomics and Memory Ordering

10,531 views
•Apr 2, 2021

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

You can read more about Rust's memory model in the Rust reference at https://doc.rust-lang.org/nightly/ref...​ and in the C++ memory model documentation at https://en.cppreference.com/w/cpp/ato...​.

Live version with chat: https://www.youtube.com/watch?v=0Vq2a...

Show lessShow more

41 Comments

Sort by
default-user=s48-c-k-c0x00ffffff-no-rj
Add a public comment...
"Don't use spinlocks! Don't implement spinlocks! Ok, let's implement a spinlock". Love it!

1 week ago

Jeff Preshing has an incredible blog (and I think some videos) on this. He's a C developer, but as you said that doesn't matter.

1 week ago

omg I will need to see this video 5 times to understand half

6 days ago

Very interesting, thanks. The general rule seems to be (as with crypto): don't roll your own (if you can avoid it).

1 week ago

I want to give feedback:

This is great. It fits my difficulty level perfectly and is exactly what I need - as the 2018 guideline suggests.

Im have some experience with rust and am a computer science student currently just before my bachelors degree, so I have some experience with programming and how a computer works on several levels. Heck, I wrote a small subset-of-c-compiler. But I i.e. never heard of Ordering before. Those topics normally have a super high barrier of entry because of all the previous knowledge required, only that that knowledge has the same barrier. Your videos help break that and to learn a lot about how computers work in general.

Keep on with the great work. I wish all my lectures were even half as engaging, informative, accessible and interesting.

Read more 1 week ago

This video exposes one concept that you rarely notice in regular code. Computer code looks sequential, when written down as text, but in actuality it defines an oriented graph of operations. If two operations don't lie on the same path through the edges of the graph, their relative order is undefined. Multi-threaded memory access exposes this weird quirk, because it itself may lack inherent ordering (ie. allows for race conditions). Memory ordering introduces extra edges to the graph, to restrict possible orderings.

Read more 1 week ago


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK