Image missing.
Fun with Futex

created: June 3, 2025, 6:37 a.m. | updated: June 4, 2025, 12:55 a.m.

We are going to take a look how one can implement a simple spin lock in C (just like the spin lock in Go I implemented a while back) and then a slightly more elaborate lock using operating system’s primitives. } void unlock ( spin_lock_t * lock) { atomic_store ( & lock -> locked, false); }You don’t really need more than that for a simple spin lock. Mutex implementation using futexLet’s start building our new mutex! Now not only our the total elapsed time of our futex lock implementation is lower, we’ve used much less CPU time. Memory ordering: I’ve written one implementation of futex lock in C that uses acquire ordering for locks and release for unlock.

5 days, 9 hours ago: Hacker News