by avl999 · 4y ago · 0 comments
I have seen Go code that twists and contorts itself to solely use channels when straightforward mutexes would have made the code easier to understand and reason about.
Libraries like this and channels should only be seen as another tool to consider when writing concurrent software rather than treating them like the panacea for concurrent software.
This approach can be taken too far. Reference counts may be best done
by putting a mutex around an integer variable, for instance.
But as a high-level approach, using channels to control access makes it easier
to write clear, correct programs.
I'm sure somebody has been banging a "Go programs should never use mutexes" drum, but it hasn't ever been the position of the Go team that channels are a panacea or that mutexes _shouldn't_ be used, as far as I've ever seen.It feels like a lesser sin to use channels in clever ways to recreate mutexes or other patterns than the other way around. But it’s definitely true that they are juiced up to make tradeoffs that are morally wrong for some things.
TLDR; CSP is a great model for concurrency but barely anyone uses it.
I've read it after inventing similar concept and implementing it on top of Python+Redis. So embarrassing.