4 ms·
> If a multithreaded program is unreliable it’s most likely due to the same reasons that single-threaded programs fail: The programmer didn’t follow basic, well
by rdtsc 13y ago
> If a multithreaded program is unreliable it’s most likely due to the same reasons that single-threaded programs fail: The programmer didn’t follow basic, well known development practices.
Well no. It is like saying mt programs are broken because the user didn't know how to write them. It is not saying much. Everything with bugs is broken because the writer didn't know how to write it.
The reality is that multi-threaded is hard because it is hard to reason about and hard to get right. In one large application it only takes on junior programmer to dip their fingers in and add that one module that will crash the system 1 out 1000 run on customer's hardware only.
MT issues and problems on a whole new level of difficulty that is qualitatively different than what single threaded programs experience. Sequential problems and bugs are still there, but now there are a host of completely new things happening.
> These programs very often fit nicely into a producer-consumer model with three threads:
* The input thread reads data and places it on the input queue.
Well, you also have to make sure the queue is thread safe. Understand what does it mean to get an item off of an empty queue. Now you have queue-using-up-all-my-memory problem if you do stream processing with mis-matching processing rates between parts of your programs. What about objects you put in the queue. If those have any pointers or are pointers themselves now you are back to the original problem. So make sure objects are copies, are serialized/deserialized or there is proper ownership handoff.
In summary, is it possible to write correct highly concurrent programs in a shared memory model with threads/and-or/events -- yes. Is it easy? No, it is not easy.
The article correctly points the often common fallacy that using event/callback-based single threaded approach is superior and avoids all the concurrency related issue. It doesn't avoid all issues. Now the race is between callback/errback chains instead of threads. Granularity is much higher (if incrementing a shared 64 bit int is fine without a lock). But the need to protect shared data structures is still there. The danger is psychological because one thinks "hey this is safe, I don't need anything to worry about"
That is why I believe it is better to use a language/system/framework that uses isolated heaps and rigidly enforces separation between concurrency units. Erlang has that, D has some of that, Nimrod, Web Workers and Dart isolates to that. You incur a speed penalty for that. Don't be shy about using regular Unix OS processes. Those are great for isolation and do it well. The cost of IPC and cost of creating them can also be high. You in incur a penalty for copying data between concurrency units, but remember, there is a bigger penalty you incur when the program has crashed. The converse of that is, the biggest performance gain your application will achieve is when it goes from not working to working and staying working.
- rbehrends 13y agoIs it easy? No, it is not easy. Correction: It's not easy in most programming languages, because most programming languages fail at implementing mutual exclusion in a way that is safe. It's basically the same problem as buffer overruns in C, where indices don't get checked (except that in the concurrent case, most languages don't require you to associate a lock with shared data and/or don't verify that you hold the lock before operating on that shared data). Per Brinch Hansen's famous rant gives the gory details [1]. The basic technology had been figured out in the early 1970s. [1] http://brinch-hansen.net/papers/1999b.pdf http://brinch-hansen.net/papers/1999b.pdf
- Jtsummers 13y agoTrapped behind a web filter so your link was filtered as "Personal Site". I believe [1] is the same document for others in the same situation. Also, thanks for the link, it's an interesting read. [1] http://cse.unl.edu/~witty/class/csce351/howto/PBHansenParallelism.pdf http://cse.unl.edu/~witty/class/csce351/howto/PBHansenParall...
- notacoward 13y agoI agree with "hard to reason about and get right" because of the state-space explosion. If you do five atomic things in a single-threaded program, you have six total states to deal with. If you do those same five things in five threads, you have over 200. That's true regardless of whether you're using a naive "locks and blocks" model or a sophisticated functional/CSP/actor model, and splattering that state across multiple stacks/continuations doesn't exactly make debugging easier. People who have done this for a while do apply good programming practices to reduce the number of states and the dependencies between them, but the fundamental problem still remains. Every new state represents a new potential for error, and code that has many error cases is simply harder to write/debug/verify than code that has few.
- VLM 13y ago"That's true regardless of whether you're using a naive "locks and blocks" model or a sophisticated functional/CSP/actor model, and splattering that state across multiple stacks/continuations doesn't exactly make debugging easier." Enormously greater complexity has the side effect of making execution slower. Something to think about. Converting a fast executing and quick to write/debug and reliable single threaded app into an inherently slow executing and slow to write/debug and unreliable multithreaded app needs an extremely good reason outside of the tradeoffs listed above. Those situations do exist, but situations like "I thought it would be fun to put multi-threaded programmer on my resume" happen more often. Its kind of like using "goto". If you don't really understand everything about the topic, its probably a wise choice to always avoid.