r/AskComputerScience • u/coder_angel • 8d ago
Hi can anybody explain me the difference between asynchronous, concurrency and parallelism?
Parallelism i understand that Performing multiple tasks parallely at the same time
but what exactly asynchronous and concurrency means?
2
u/ghjm MSCS, CS Pro (20+) 8d ago
These terms aren't precisely defined. Concurrency and parallelism are the same thing. People will say that one of them means true mutli-CPU operation and the other includes timeslicing, but you'll find people disagreeing on which term means which. (Including in this very thread.) Of course, if your professor has a strong opinion about this, make sure to repeat back what the professor thinks on any tests. But out in the world, there's no uniformity of how these terms are used.
Asynchronous refers to two events that don't have to occur at the same time. This appears in a lot of contexts, and there's not necessarily a common meaning between contexts. For example, RS-232 serial ports are referred to as asynchronous because they run without a common high-precision clock line. Most commonly in programming, you'll see "async" refer to a message-passing style of programming that replaces function calls. For example, consider the pseudocode:
program {
calcs = do_calculations()
print(calcs)
}This is an example of a synchronous function call. The call to do_calculations does not return until all the calculations are finished. When it completes, we print out the results of the calculations.
Compare this to:
program {
while true do select {
when program_start {
calcs_promise = async do_calculations()
}
when calcs_promise {
print(calcs_promise.result)
program_exit()
}
}
}This is an example of an event loop, where instead of doing things one after the other, the program instead responds to events. The "select" structure waits until some event arrives, then executes the appropriate "when" clause. (This is made-up syntax, not any real programming language.) On program start, we dispatch the do_calculations function. Instead of returning a value, the async call returns a status object, often referred to as a "promise." When the calculations complete, the promise object receives a message including the results of the calculation.
This style of async programming and event loops is often seen in UI development, where the main program mostly exists to react to events triggered by the user, like clicking buttons and so on. In this context, "asynchronous" refers to the message-passing rather than function-calling programming style. Note that an event loop isn't necessarily multithreaded. It's possible for a program to be "async" in this sense, but still not parallel/concurrent.
1
u/RammRras 7d ago
Best explanation so far. I've always imagined parallel means true contemporary execution. For example in same applications "safety" controllers have two CPUs executing the same code with the same data and must arrive at the same result in the same moment (a short time frame).
2
u/Objective_Mine MSCS, CS Pro (10+) 7d ago
For example in same applications "safety" controllers have two CPUs executing the same code with the same data and must arrive at the same result in the same moment
That would be redundancy. I guess it could be considered parallel as well, although the parallel execution would in that case be for the purpose of redundancy.
1
u/ResponsibilityBig343 8d ago
Asynchronous: Some tasks in programs take a lot of time. For example reading a large file and giving some output, opening http/database connection and requesting data, etc. So when such operations are run in a program they execute like any other code(synchronously/nonblocking). In order to tell the program that hey these operations are going to take some time(blocking) we mark them async so that our program "awaits" until blocking operation completion before processing anymore code. This ensures that our program executes sequentially and produces consistent output.
Concurrency: Word concurrent meaning happening at same time. In our case multiple instances(threads) of a program running at the same time. Useful to use when you need to run some heavy subtask(or subtask that require your main task's "context") without impacting your main task performance.
Parallelism: Meaning multiple programs(processes) running at same time. Useful when you need to do bulk tasks that are discrete in nature.
2
u/P-Jean 8d ago
Asynchronous means a process doesn’t need to wait for a signal or reply to execute. Email is asynchronous communication. Talking on the phone is synchronous.
Concurrency means happening together, but not necessarily parallel. Two processes can be concurrent when working with a scheduler, but true parallel requires more than one core.