Diving into concurrency: trying out mutexes and atomics
Posted on by Rust on Julia Evans
I hadn’t written any threaded programs before yesterday. I knew sort of abstractly about some concurrency concepts (mutexes! people say compare-and-swap but I don’t totally get it!), but actually understanding a Thing is hard if I’ve never done it. So yesterday I decided to write a program with threads! In this post, we’re going to:
- Write a threaded program that gets the wrong answer because of a race condition
- Fix that race condition in C and Rust, using 2 different approaches (mutexes and atomics)
- Find out why Rust is slower than C
- Talk a little about the actual system calls and instructions that make some of this work