Hell is a multi-threaded C++ program (2006)
I&#x27;ve been writing massively threaded C++ code since the 1990&#x27;s, so I can comment with some serious experience here. My code used to run on 256CPU SGI&#x27;s and had high cpu utilization across all of them.<p>When designing C++ code for threading, you have to keep a few things in mind, but it&#x27;s not particularly harder than on other languages with thread support. You generally want to create re-entrant functions which pass all of their state as input, and return all results as output, without modifying ant kind of global state. You can run any number of these in parallel without thread safety issues, or locks. When using multiple threads with shared data, you have to protect it with locks, but I generally use a single thread to access data of this sort which produces work for other threads to do, which can be queued or distributed any other way. Producer&#x2F;consumer queues show up a lot in good designs, and your single-threaded choke points are usually a producer or a consumer, which allows thread fan-in or fan-out. (As a side note, Go really got this right with channels).<p>What leads to problems in threaded C++ programs is unprotected, shared state. Some C++ design patterns make life particularly hard, such as singletons, which are just a funny name for global variables. C++ gives you lots of ways to make something global accidentally - static member variables, arguments by reference, for instance.<p>True C++ hell is taking a single threaded C++ program and making it multi-threaded after the fact, but that&#x27;s hell in any language which allows you to have globals of any sort and doesn&#x27;t perform function calls within some kind of closure.<p>The article in this post is being a little reductionist. You don&#x27;t need to pick a threading model - some of your threads can pass messages, others can use shared data, all in the same program. The lack of thread local storage is not an issue, because you partition the data so that no two threads are working on the same thing at the same time.