2 ms·
"The reality is that multi-threaded is hard because it is hard to reason about " I think that's key. The primitives of multithreading aren't difficult, nor a
by GrinningFool 13y ago
"The reality is that multi-threaded is hard because it is hard to reason about "
I think that's key.
The primitives of multithreading aren't difficult, nor are the basic concepts. What is hard for most people is thinking about it in a meaningful way.
The mental model for single-threaded programming is challenging for many - you essentially run the program or subset of the program in your head (at least, if you're any good). This includes all branching and jumping, and effectively requires maintaining one or more stacks among other things.[1]
To fully conceptualize multi-threaded programming, programmers need to be able to expand their mental model of the executing application. Not holding one stack, but N. Awareness that each may (and likely will) differ slightly. Awareness of other actors. Awareness of exactly what is shared, and what paths lead to its modification. Being able to visualize (for lack of a better word) which others may interact with the shared data, and under what circumstances. All of this comes into play when you add just a second thread executing the same code - it gets considerably more complex from there. The closest analogy that comes to mind is the shift from thinking in three dimensions from two, or four from three.
It is not impossible to reason about, nor is it impossible to build such a mental model. But it is hard. And it does take practice.
[1] And even this basic ability seems unfortunately uncommon.