Barrier: Multithreading bugs are very delicate
ridiculousfish.com
ridiculousfish.com
The Ridiculous Fish guy is clearly very smart and writes lots of interesting stuff, but this is obviously not his area of expertise, and you can't afford to learn from someone who has any confusion on the topic.
In his conclusion he makes what I would consider a highly misleading comparison between locks and memory barriers. He calls locks "tanks" ("powerful, slow, safe, expensive, and prone to getting you stuck"). About memory barriers he says: "Memory barriers are a faster, non-blocking, deadlock free alternative to locks. They take more thought, and aren’t always applicable, but your code’ll be faster and scale better."
But memory barriers aren't an alternative to locks at all. Locks let multiple threads write to shared memory. Memory barriers by themselves aren't very useful; most lock-free algorithms need atomic operations like compare-and-swap, which are comparable in cost to locks (indeed, locks are implemented in terms of atomic operations).
Also, a surprisingly hard problem with lock free data structures is knowing when you can free/unmap any of their memory. Since there is no mutual exclusion, there is no way of knowing that another thread didn't just read the address of the thing you want to delete. He could read that address and then get swapped out for 100 years, and you can't unmap that memory until he gets rescheduled and finishes his load. Maged Michael published a technique for dealing with this problem he calls "Safe Memory Reclamation" or SMR.
Don't get me wrong, I like lock-free data structures. I just think it's important to understand that they have their issues too, and it's not as though everyone should replace all their mutexes with lock-free structures.
I also think it's important to realize that atomic operations and memory barriers are not application-level constructs as mutexes are. People should leave the atomic operations and memory barriers to the experts, and only use higher-level abstractions in applications, like lock-free stack, queue, etc. You wouldn't dream of implementing a mutex yourself in real code; the same should be true of using atomic operations or memory barriers, unless you're really an expert. One possible exception is atomic increment and decrement for simple reference counting.
That and the Alpha having a split cache where one half can be more up-to-date than the other, really sound like a lot of fun.
[1] I tend to use processes and IPC whenever I can, for example.
[1] http://www.thecodist.com/article/writing-multithreaded-code-...
If you have a lot of critical sections all over the place, you're probably doing it wrong.
The only exception is when you're forced to implement network code using a thread-per-connection or thread-per-socket model, in which case you might end up having to have your client/server threads work within your app's regular workflow. Icky, and should be avoided when possible.
I did say "I feel" because I could be wrong, but there are still a lot of people who think that the operating idea of a thread is still the only possible meaning of the term (I meet them every time Node.js comes up and the Node.js partisans argue passionately against operating system threads), but those days are long gone. So even if you do understand this, there are others who don't.
- Don't write this stuff on your own.
- Use someone else's mutex, and someone else's non-blocking datastructure.
- Really, don't write your own.
Threading is not that hard unless you insist on leaving the safety off and aiming it at your foot.