Building a Tiny Mutex
kprotty.me·176w

Lets say you’re writing your own synchronization primitives for whatever reason and you need a small, fast Mutex.

Could be that you made your own OS and pthread’s API ain’t looking too good. Could be that you want something faster than what your platform’s libc provides through pthread. Could be that you’re doing it for fun (the best reason), it doesn’t matter.

In a perfect world, you just write a simple spin-lock and be done with it. But scheduling isn’t that easy and such naïve solutions can have pretty bad or awkward consequences. In this post I’ll walk through designing and understanding what makes a good mutex. I’ll assume you know some about atomic memory operations and their memory orderings (pretty thicc assumption, I know).

Spin Locks

You love to see ‘em. First…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help