Slashdot Log In
Multi-threaded Programming Makes You Crazy?
Posted by
Hemos
on Tue May 02, 2006 10:18 AM
from the doing-it-better dept.
from the doing-it-better dept.
gduranceau writes "Help! My program deadlocks! I got several concurrent threads that write the same variable! Everything goes well on my mono processor but becomes an incredible mess on that 16 CPU monster! And of course, as soon as I add traces, problems disappear... Don't panic! Calm down and take a deep breath. "
Related Stories
[+]
Technology: Donald Knuth Rips On Unit Tests and More 556 comments
eldavojohn writes "You may be familiar with Donald Knuth from his famous Art of Computer Programming books but he's also the father of TeX and, arguably, one of the founders of open source. There's an interesting interview where he says a lot of stuff I wouldn't have predicted. One of the first surprises to me was that he didn't seem to be a huge proponent of unit tests. I use JUnit to test parts of my projects maybe 200 times a day but Knuth calls that kind of practice a 'waste of time' and claims 'nothing needs to be "mocked up."' He also states that methods to write software to take advantage of parallel programming hardware (like multi-core systems that we've discussed) are too difficult for him to tackle due to ever-changing hardware. He even goes so far as to vent about his unhappiness toward chipmakers for forcing us into the multicore realm. He pitches his idea of 'literate programming' which I must admit I've never heard of but find it intriguing. At the end, he even remarks on his adage that young people shouldn't do things just because they're trendy. Whether you love him or hate him, he sure has some interesting/flame-bait things to say."
This discussion has been archived.
No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
Full
Abbreviated
Hidden
Loading... please wait.

Use the right tool (Score:4, Interesting)
Oh wait. I was supposed to praise the NPTL tool, wasn't I. Um... well... it's very nice. And they've got... um... penguins on the homepage. Oh, and look! It's GPLed! Wow. Just... um... wow. Hey, did you know that the author of Minix wrote a book [amazon.com] on OS Design? Really. It even covers the basics of multi-threading. It's pretty cool, you should... um... check it out. Yeah, that's the ticket!
Java no panacea -- must know what you're doing (Score:5, Informative)
First off, that in itself will not prevent deadlock. Secondly, it's damned inefficient.
Look: there's just no way around it. If you want to do effective (i.e. low bug, high performance) multithreaded programming, you simply have to understand what you're doing. Ultimately, the tools of your trade will be mutexes, condition variables, semaphores, etc -- the O/S primitives. Don't rely on your programming language to "automatically" use these for you, blasting out mutexes machinegun-style. Instead, figure out the logic of your program. You probably need only a small number of mutexes.
A key to effective multithreaded programming is to adhere rigidly to certain programming practices. It must _NEVER_ be the case that 2 threads have write access to a given item at the same time. Duh. But you can use fancy programming tricks to, in effect, automatically add run-time assertions to your code which assure that this practice is being adhered to. In production mode, you remove these runtime assertions.
Another good practice is, if you really need to have multiple mutexes, to arrange them into a hierarchy. When a top-level mutex is locked, no other mutex can be locked. When a second-level mutex is locked, only top-level mutexes can be locked. Etc. This hierarchy can be verified at runtime, in debug mode. Adhering to this regime will go a long way to removing the possibility for deadlocks.
Bottom line: you really have to know what you're doing in order to write good multi-threaded code. You should take the time to really study that problem space. An excellent book I've found for this purpose is "Concurrent Programming in ML". (I know -- nobody uses ML. So what? Learn the language just for the purpose of understanding the book. Then, you can apply your knowledge to any domain you're working in).
Parent
Re:Java no panacea -- must know what you're doing (Score:5, Informative)
Batman was right that after using Java's threading this NTPL trace looks pretty lame. Not only is the threading and locking in Java braindead simple, but the JVM actually tells you what is wrong. For instance it detects deadlocks and gives you the complete call trace of each deadlocked thread.
Other languages have good locking too (ruby for instance), so it's more that everything is difficult and crappy in C and its kind. I guess if you are stuck writing a threaded application in C in the first place this tracing library could be useful. Of course if you use the heap you're going to also want to replace malloc/free with a fast multithreaded version and then do a bunch of hacks so that it isn't ridiculously slow (locking on every free()... now *that's* inefficient).
Parent
Re:Java no panacea -- must know what you're doing (Score:3, Informative)
Yes what you say it true, but these are all implications of atomic read/write... It follows logically from having caches
Umm, no, it has nothing to do with atomicity, nor is it because of caching.
Atomicity just guarantees that the atomic change is seen
Re:Java no panacea -- must know what you're doing (Score:5, Informative)
It must _NEVER_ be the case that 2 threads have write access to a given item at the same time.
Two clarifications:
First, it's okay to allow multiple threads write access as long as the writes are guaranteed to be atomic and as long as the order of atomic writes doesn't matter. In practice, that second restriction usually means you need locking.
Second, it's often important that one thread not be writing an object while one or more threads are reading it. In other words, multiple writers aren't the only problem.
Parent
Re:Use the right tool (Score:3, Insightful)
Re:Use the right tool (Score:5, Informative)
The correct tool is called a brain, but first the brain must be configured properly.
Deadlocks are one symptom of poor program logic, and are designed into the program due to lack of proper controls. They frequently occur when a program is not designed before it is written.
See "dining philosophers [google.com]" for an explanation of this, and several methods to prevent this situation.
Tracing tools are all well and good, but if one starts out with correct logic in the first place then one won't spend more time debugging than programming.
Always remember that a digital computer is a logical computing device. If you give it a series of instructions which do not ALWAYS have a logical solution, then it will choke
-Adam
Parent
Re:Use the right tool (Score:3, Informative)
There are some good tools for the job. Relatively speaking, Java isn't one of them.
While Java does include some built-in support for multithreading primitives, its underlying model (using locks on data to prevent simultaneous access) is the same as many
Re:Use the right tool (Score:3, Insightful)
Really when are people going to get over this multithreading problem? Concurrency
Re:Use the right tool (Score:3, Informative)
All good programmers... (Score:5, Funny)
Re:All good programmers... (Score:3, Funny)
Go crazy?
Don't mind if I do!!!
Multi-threaded Programmation Makes Me Crazy? (Score:4, Funny)
Re:Multi-threaded Programmation Makes Me Crazy? (Score:4, Informative)
Notice also: take a deep *breathe*.
But I agree... multi-threaded programming can drive people crazy. Message passing-based programming is less prone to nastiness than shared-state concurrency. (Languages like Erlang come to mind).
http://en.wikipedia.org/wiki/Concurrent_programmi
http://www2.info.ucl.ac.be/people/PVR/bookcc.html [ucl.ac.be]
You can also do Erlang-style message passing in Python using Candygram
http://candygram.sourceforge.net/ [sourceforge.net]
Parent
Re:Multi-threaded Programmation Makes Me Crazy? (Score:5, Funny)
Parent
Link to the home page would be nice (Score:5, Informative)
Probably going to download it anyway... (Score:4, Interesting)
Looking at the list of functions that it hooks into, I don't see pthread_rwlock*. Are the pthread_rwlock functions implemented using other pthread_* funcs? I haven't run into any problems yet with the project I'm working on, but it would be nice to run through this and make sure everything's working as expected.
Bah (Score:3, Informative)
The only good general advice about learning how to develop software on distributed systems I can give is: Read some of Andrew S Tanenbaum's books about operating systems and distributed systems in particular. The books contain knowledge you'll be able to apply to almost every system you develop software for.
Why all the negativism in the posts? (Score:5, Insightful)
Multithreaded Haiku (Score:3, Informative)
valgrind used to be able to do this (Score:3, Insightful)
personally, i can't say enough good things about valgrind. there are a couple non-obvious issues (support for sse/sse2/sse3 is still in the works, so if you get an inexplicable SIGILL, this is probably the problem), but it's saved me hundreds of hours over the past year (and i'm sure it'll save me even more in the future).
that all said, my (admittedly limited) experience with threading is that it's best to design the deadlocks away before you even touch the editor. i wonder if there are any design tools which support deadlock / contention checking at the model or design level?
No that's just common sense. (Score:3, Insightful)
That's not "limited" experience. That's common sense. Trying to find deadlocks, race conditions, and accident
Re:valgrind used to be able to do this (Score:3, Informative)
At my previous company we built a system with on the order of 10 threads working on a combined dataset consisting of many hundreds of thousands of objects and occupying a couple hundred meg of memory in a large installation. There could be hundreds of thou
Re:Here's your problem... (Score:3, Interesting)
http://en.wikipedia.org/wiki/Concurrent_computing [wikipedia.org]
http://en.wikipedia.org/wiki/Message_passing [wikipedia.org]
http://en.wikipedia.org/wiki/Actor_model [wikipedia.org]
Re:Here's your problem... (Score:4, Informative)
Your point that message passing is generally a cleaner design choice is valid, but it's not always a practical option.
Parent