logoalt Hacker News

EdSchoutenlast Monday at 6:24 AM2 repliesview on HN

I don’t understand why I would need to care about this. Can’t my operating system and/or pthread library sort this out by itself?


Replies

senderistalast Monday at 6:56 AM

Pretty much, given that any decent pthreads implementation will offer an adaptive mutex. Unless you really need a mutex the size of a single bit or byte (which likely implies false sharing), there's little reason to ever use a pure spinlock, since a mutex with adaptive spinning (up to context switch latency) gives you the same performance for short critical sections without the disastrous worst-case behavior.

baobunlast Monday at 6:57 AM

General heuristics only get you so far and at the limit come with their own overhead compared to what you can do with a tailored solution with knowledge about your usage and data access patterns. The cases where this makes a practical difference for higher-level apps are rare but they exist.