4 ms·
So to simplify, what I'm gathering from this, and other comments, is: Concurrency occurs when program semantics allow two threads of execution to be executed i
by javert 16y ago
So to simplify, what I'm gathering from this, and other comments, is:
Concurrency occurs when program semantics allow two threads of execution to be executed in a simultaneous or interleaved manner.
Parallelism occurs when two program threads actually execute simultaneously.
By the way, I still have problems with the use of "non-determinism" here. Contrary to what you say, schedulers do not schedule threads randomly. But looked at from the perspectice of program semantics, that might as well be the case, which is (I think) the reason people keep bringing that up.
- HenryR 16y agoAs you say, from the perspective of the program any scheduling interleaving is possible. The OS can substitute a new scheduling algorithm any time it likes. Otherwise we could know that the possible set of executions is much smaller than the set of all interleavings, and could write our concurrent code based upon that assumption. Then we'd be SOL when anyone tweaked the scheduler.
- javert 16y agoWhat do you think about the definitions I gave of the two terms? BTW, I'm going to pick on you a bit. I don't like it when people oversimplify things in CS. I think it can be confusing to beginners (and, sometimes, non-beginners). I don't like when people say things like "the scheduler can do whatever it wants," "the OS scheduler can change any time," "the scheduler is non-deterministic," etc., because I know those things aren't true. They may help make a point, but it's also introducing a source of potential confusion. I say this from the perspective of somebody who has actually done some work on OS schedulers. I mean, to me, the OS never substitutes a new scheduling algorithm any time it likes, though an OS hacker may actually change the algorithm. And, as you may know, kernel hackers take advantage of known limitatations in concurrency (i.e., concurrency that will never "actually happen").
- ww520 16y agoI don't see how it confuses people. In CS, the important thing is to specify the contract between subsystems. To your program, a preemptive scheduler's contract is it will run your threads in any order it wants. The point is not whether it's truly random or pseudo random. The point is there is no guarantee of ordering. You better deal with it. You cannot rely on the threads running in certain order. That's non-deterministic. It's a basic notion in concurrency. Note that I never said the scheduler is non-deterministic. It is the order of your threads' execution that's non-deterministic.
- javert 16y agoIt confuses people because (a) it's not true and (b) it's beside the point. There are lots of schedulers out there that have a different contract than the one you described ("run threads in any order"). For example, many real-time systems run threads in priority order - you only get preempted if a higher-priority thread wants to run.
- HenryR 16y agoRight, but if you make assumptions about the scheduler, you are tied to that scheduler's implementation, which introduces a significant coupling between OS implementation and language runtime that doesn't win you much and commits you to an implementation that, honestly, can and will change over time.
- javert 16y agoYou wouldn't want to make assumptions about the scheduler if you're programming for a desktop, smartphone, or server, but you have to if you're programming a lot of kinds of embedded systems.
- ww520 16y agoI think we are getting off-track here. The non-deterministic nature is about concurrent programs, not about scheduler. Scheduler is just one source causing non-deterministic execution order of threads. There are other non-deterministic causing sources. Imagine you spin off 10 threads waiting for network requests where one thread per client machine. It cannot be deterministically decided which thread will run first and which will run next. It depends on which client connects to which thread first. That's the non-deterministic nature of the program. Concurrency is the discipline to deal with that problem.
- HenryR 16y agoI think your definitions are good, although thread-centric - to be really precise you have to talk a bit about 'steps' in the operational semantics of the program, but it's not helpful usually to do so.
- javert 16y agoI do think it would have been better if I had said "contexts of execution" wherever I said "thread."
- ww520 16y agoThe key term to concurrency is the non-deterministic order of execution of multiple threads. Non-deterministic means that you run the program one time, the order of execution is: thread 3, thread 5, thread 1, thread 4, and thread 2. You run it another time, the order can be: thread 1, thread 5, thread 2, thread 4, and thread 3. In another time with 2 CPU: thread 1/5, thread 1/3 (1 overlaps 5 and 3), thread 2, and thread 4. From your perspective, the OS schedules your threads "randomly." It might be due to system load, interrupt, or timing. Doesn't matter. It's just that the order is not guaranteed. The goal of writing a proper concurrent program is no matter what the order of execution, the computed outcome is the same, due to consistency guarantee coded in.
- javert 16y agoThat's wrong because there's real-time systems out there that run threads in priority order. Are you saying that such a system does not have concurrency?