|
Apr 29
2008
|
Threads considered harmfulPosted by James Reinders in Programming Style, Parallelism Concurrency, concurrency, abstraction |
Many have or will shortly embark on the adventure of writing parallel programs. It is, therefore, useful to reflect on what mistakes we might avoid.
My favorite quote is one often attributed to Mark Twain as "History may not repeat itself, but it does rhyme a lot."
Reflecting on a seminal paper (letter) some 40 years ago, "Goto Statement Considered Harmful " by Edsger Dijkstra published in Communicatons of the ACM in March 1968, I see a rhyme in the making.
Dijkstra's letter was short, and included the statement "The go to statement as it stands is just too primitive; it is too much an invitation to make a mess of one's program."
His paper is widely considered to have pushed the industry to a higher level of abstraction - specifically to structured programming (as in block-structured languages).
I still remember vividly those who argued years after his paper was published that eliminating the 'goto' statement would infringe on creativity. In fact, a series of published arguments in favor of the Goto Statement created a response from Dijkstra 20 years after his initial letter entitled "On a somewhat disappointing correspondence" in which he said "The whole correspondence was carried out at a level that vividly reminded me of the intellectual climate of twenty years ago, as if stagnation were the major characteristic of the computing profession, and that was a disappointment."
By now, we certainly know that some programs fail to pass the test of time. Such programs will be re-written because they are too bound to the hardware, embody hard to maintain programming styles, or otherwise express things in a complex manner which can be stated more easily using newer methods.
Today, we have many parallel programs. They are often written using native threads (pthreads , Windows threads, Java threads or Boost threads ).
In the end, they make a mess of a program. Threads are necessary plumbing, but in applications they simply result in programs which will not pass the test of time. Parallel programming, in order to stand the test of time, needs higher level programming methods.
The practical side of me suggests use of any threaded library, OpenMP or TBB. A less practical side encourages looking at new programming languages like Erlang or Haskell. Either way, the more abstact expression of a program will hold up better over time than the lower level of programming associated with native threads.
Are we going to rhyme, and write lots of parallel code with the modern equivalent of the "Goto Statement"?

written by James Merritt, April 30, 2008
The link takes you to a citation page, which also includes a direct link to the 17-page PDF version.
Lee worries about the proliferation of non-deterministic complexity that threads can create, observes that we have been implicitly restricting threads by our programming practices on the one hand and by the relative paucity of true parallel computing environments on the other, and predicts that as soon as computers routinely begin to have more than a couple of processing cores, we will begin to encounter some serious and unpredictable bugs that currently lie dormant in all the threaded code that is already out there.
He does have a prescription for fixing the problem, though.
written by Mark Nelson, April 30, 2008
Threads must be relegated to the engine room of computing, to be suffered only by expert technology providers.
My most successful heavily-threaded C++ programs have been based on a fairly strict message passing paradigm, which protects you against a lot of problems - no poking into each other's data. But when you do that, you are almost running as if your threads were separate processes, so you get less of an advantage from the threading model. Faster context-switching, I guess, and a somewhat lighter load on the system.
And of course, if we really believe that the multicore universe is a bigger threat that global warming and the RIAA combined, (don't count me in http://marknelson.us/2007/07/30/multicore-panic/), threading really doesn't do much to help you partition your problems.
written by James Merritt, April 30, 2008
"And of course, if we really believe that the multicore universe is a bigger threat that global warming and the RIAA combined..." You forgot Y2K.
written by Kevin Queen, May 02, 2008










I agree that threads have to disappear into the infrastructure, just like GOTOs disappeared into the compiled code (but snuck back in via exceptions).
But I also harbor a lingering doubt that Knuth is correct, perhaps parallelism is a swamp we just want to stay out of. Just because it's a bandwagon doesn't mean we should necessarily be jumping on.
- Mark