4 ms·
Concurrent programming is hard because "concurrent programming" usually means "concurrent programming with mutable states and imperative control constructs". So
by kazinator 12y ago
Concurrent programming is hard because "concurrent programming" usually means "concurrent programming with mutable states and imperative control constructs". So concurrent programming is hard because imperative programming is already hard, and then multiple threads with unpredictable scheduling order are let loose on it.
Also, due to variation in scheduling order, concurrent program's bugs may be only sometimes reproducible, even when the input between the working and failed cases are identical. It's as if all concurrent programs have an implicit real-time input source that perturbs their behavior.
The bugs in concurrent programs are an entirely new class, like "race condition", "lost wakeup" and "deadlock". Even a great programmer who understands N levels of pointer dereferencing and "leaps complicated algorithms in a single bound", but has no concurrent experience, may well be defeated by concurrency bugs.
- trhway 12y ago>Concurrent programming is hard because "concurrent programming" usually means "concurrent programming with mutable states and imperative control constructs" people have trouble even with database transactions - the simplest "imperative with mutable" concurrency model around. The simplicity comes in particular from well-defined hierarchy of objects and locking semantics upon them. No wonder that programs with concurrency, especially written in OOP languages where mutable state hidden, spread around in really disorganized way and de-facto inter-mixed with code, is just a mess. The article mentions Netbeans - yep, UI OOP models are among the messiest.
- kazinator 12y agoI'd like to expand on this just a little; indulge me. When we write programs, we normally keep them tidy by taking advantage of scoping. Data is localized using local variables. We use parameter passing to communicate among modules and avoid globals. However, imperative concurrent programming is all about globals: the "shared variables" that have to do with the concurrency of the program, violate scoping: necessarily so, because to be shared, they have to be accessible from multiple scopes. In imperative programming, there is an important reassurance: namely that variables retain their last stored value. If you put 42 into x, then x keeps holding 42 until you put something else in x. If x is globally visible, and you call some function, forgetting that it has a side effect of changing x, it wrecks this assumption. Suddenly, x has mysteriously changed: you could swear you put in 42, but now it somehow has 43. Why? All you did was call foo; that has nothing to do with x. Oh, but someone committed a change, and foo now calls bar, and bar makes several other functiosn calls which end up back in this same unit in another function that frobs x. In concurrent programming, you are opening the doors to this type of problem: and what is worse is that shared variable x can change at any time, not over some predictable action like calling a function. When you assign 42 to x, x could turn into 43 even before control passes out of the assignment statement and to the next statement. All the surprises arising from multiple scopes having visibility over the same shared data become worse thanks to the asynchronous nature and unpredictable scheduling order. If you keep programming in the same way that you are used to, you are guaranteed to have a problem. Simply to have the same assurance that "x is holding the value I put there in the current thread of control" you need to take special steps, like locking a mutex (consistently, from every user of x) whereas in non-concurrent programming, the assurance comes from good design, like making x local, or simply not branching into any code which might change x for as long as you need x not to change. I.e. you can achieve the basic assumption quite passively in the absence of concurrency (based on what you don't do), whereas you have to be proactively cunning in concurrent programming.
- boomlinde 12y ago> imperative concurrent programming is all about globals: the "shared variables" that have to do with the concurrency of the program, violate scoping: necessarily so, because to be shared, they have to be accessible from multiple scopes. I may be nitpicking here but "accessible from multiple scopes" is not the same as being "all about globals". Data can usually still be localized to the routines that use it without throwing it in the global scope. The inherent problem of two threads sharing data is also something that will _have to_ be dealt with on some layer of the implementation if you want the threads to communicate, regardless of programming paradigm. Some languages provide really nice abstractions to deal with this in ways that don't explicitly have the programmers invoke locks. Among imperative languages, Go provides a really useful and sensible such abstraction with channels. > In concurrent programming, you are opening the doors to this type of problem: and what is worse is that shared variable x can change at any time, not over some predictable action like calling a function. When you assign 42 to x, x could turn into 43 even before control passes out of the assignment statement and to the next statement. Still nitpicking, but you should separate the concerns of parallel programming that don't necessarily have anything to do with concurrent programming. Imperative concurrent programming can be implemented by having routines explicitly yield control to other routines (e.g. protothreads, greenlets), making the execution order entirely predictable.
- kazinator 12y agoExplicit yielding of control is not concurrent programming, almost by definition. The execution order is entirely predictable precisely because it is serialized by the yield calls. The software specifically relies on these cooperative threads not being concurrent, such as "if I don't call anything which can yield, then nothing changes in a surprising way". Also, because it is not concurrency, it won't run on multiple processors. (There can be a loose order in cooperative threading in not knowing which thread will be run next when yield is called. It's still not serialized.)
- boomlinde 12y agoAgain, note the difference between parallel execution and concurrent execution. The software specifically relies on these cooperative threads not being parallel, which is not an inherent property of being concurrent, most certainly by definition. Reading the first part of the article is a good start on understanding the distinction. Maybe this sort of lack of a basic understanding of the concepts is why concurrent programming is hard.
- jandrewrogers 12y agoSophisticated highly concurrent systems mitigate many of these problems by taking control of the scheduling order to dynamically optimize behavior. This eliminates quite a bit of coordination and agreement that you see in a lot of concurrent software. There is some interesting research on game theoretic adaptive schedulers for highly concurrent systems. Basically, if a scheduler in one process can model the behavior of schedulers in all other processes it interacts with, it can dynamically choose an operation with very low or zero probability of conflicting with the decisions made by all the other schedulers. In principle, this leads to all the schedulers getting maximum work done with minimum coordination or conflict. Some algorithms I built for massively parallel systems work this way. At some level of concurrency, agreement and coordination becomes impractical so you have to come up techniques where the individual processes make choices that ensure they do not step on each other by making assumptions about the processes they interact with.
- taeric 12y agoWhy is it that the world today is convinced it is "mutable" state that is the root of all ills? Is it difficult, yes? Should it be completely abandoned? I'm not so sure. Granted, I just recently managed to butcher[1] what is effectively an "anti-immutable" algorithm. Seeing that there have been parallel versions of this algorithm implemented successfully, I can't bring myself to throw out that tub yet. Did they do this by removing the mutable nature of the algorithm? Not to my knowledge. No, instead they kept this mutable tool and built on top of it with something that is basically message passing. That is, instead of throwing out the mutable tools we have, you can also segment the work you are giving out. This isn't exactly novel, either. Consider anyone planning the festivities of any major event. Timelines and work plans are par for the course. Yet, we often try to make things "just work" for us "in time" in programming. Still, if you want one "true" abstraction. My bet is on Channels. :) [1] http://taeric.github.io/DancingLinks.html http://taeric.github.io/DancingLinks.html
- kaoD 12y ago> Should it be completely abandoned? I'm not so sure. Nobody is trying to abandon mutable state (or nobody should, at least). If we abandoned it, our computer would be a static screen where nothing would happen. Functional programmers embrace mutable state, but only where it's needed! If the program is faster or cleaner using mutable state, why not? Check some of Clojure's mutable constructs as an example: http://clojure.org/vars http://clojure.org/vars | http://clojure.org/Refs http://clojure.org/Refs | http://clojure.org/Atoms http://clojure.org/Atoms | http://clojure.org/transients http://clojure.org/transients
- taeric 12y agoThe dream is typically, as I understand it, that the compiler can figure out all of the places where mutable state is required. Don't get me wrong, I realize most folks are more pragmatic than they are dogmatic. However, I also know that all of my coworkers and most folks I have talked to are obsessed with "persistent" collections. Often touting them as some sort of must have for reliable multithreaded work.