"I'm not proud of being a congenital pain in the ass. But I will take money for it."

Threads considered harmful

Wed 17 April 2019 | -- (permalink)

Well, OK, that's an oversimplification. Threading with shared state as most programs use it is a bad idea, and in effect postpones hard problems so that don't bite you until after you've shipped the code.

Explaining this would take quite a bit of text. Fortunately, somebody else already wrote a very thoughtful piece on this subject. Bookmark that, and read it again in a few years.

For several years now I've been a serious fan of coroutine-based non-preemptive threading in Python. I've mostly used this via the Tornado package, but Python3's async mechanisms provide essentially the same semantics, arguably in a cleaner way.

I could rant at some length about the hard sell given to pthreads decades ago by hardware manufacturers who were desperate for any new way to increase market demand for their shiny new multi-core CPUs, but that's all ancient history. Multi-core CPUs are a fine thing, just not for threading (that's why we invented processes, back when dinosaurs still roamed the machine rooms). The problem now is how to write reliable code, which is where mechanisms like Python coroutine come into the picture.