Compare and set mutex

First, let's look at a very simple mutex implementation, an atomic swap mutex that spins around in a loop until the correct conditions arrive. That is, let's build a spin-lock. This mutex is so named because every thread that blocks on lock is burning up CPU, spinning on the condition that holds it from acquiring the lock. Anyhow, you'll see. We'll lay down our Cargo.toml first:

[package] name = "synchro" version = "0.1.0" authors = ["Brian L. Troutwine <brian@troutwine.us>"] [[bin]] name = "swap_mutex" doc = false [[bin]] name = "status_demo" doc = false [[bin]] name = "mutex_status_demo" doc = false [[bin]] name = "spin_mutex_status_demo" doc = false [[bin]] name = "queue_spin" doc = false [[bin]] name = "crossbeam_queue_spin" ...

Get Hands-On Concurrency with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.