3 ms·
I'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 usin
by kazinator 12y ago
I'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.
- kazinator 12y agoThere is no substantial difference between "parallel" and "concurrent". Bot mean "running side by side" except that by "parallel processing" we usually exclude simulated concurrency. However, simulated concurrency requires asynchronous behavior: an external event, such as a timer interrupt, or the arrival of input, can come in at any time and change the control among the tasks (preemption). This looks a lot like true parallelism and must be programmed with the same care as real parallelism, which is why we dare call it concurrency. Tasking without preemption is not concurrency. The tasks know that they cannot be preempted and rely on that assumption for their correctness. This approach falls into the same domain as the use of coroutines, or dispatch queues of lexical closures, first class continuations, generators and such: none of which represent concurrency. I do not believe I require any correction in terminology or concepts; I've been programming for 33 years, and I'm the person who invented and first implemented glibc's PTHREAD_MUTEX_ADAPTIVE_NP mutex back in 2000.
- pron 12y agoI think you're attacking a straw man by very narrowly defining "imperative programming". Wikipedia says imperative programming is "a programming paradigm that describes computation in terms of statements that change a program state" (this is as opposed to declarative programming, BTW, and not not functional programming, which has no accepted definition at all). There is nothing in there saying that state has to be shared, or if it is, whether or not it can have transactional semantics. Using shared, mutable state with transactional semantics in an imperative style has none of the problems you mentioned; neither does non-shared mutable state, which is just as much a cornerstone of imperative programming as shared state. You're also implying it is possible to program without shared mutable state. To the best of my knowledge, practically every interesting program (other than compilers) has shared mutable state (yes, even Haskell programs), even if it is delegated to a database (which, of course, also has to be concurrent, and has to be programmed by someone). Now, the quest for a true general purpose STM has been abandoned, but more domain-specific forms (or even in-memory databases, which are, essentially, STM) are still very much in the game.