13

I was looking for an antonym for asynchronous (for documentation purposes) when I discovered that asynchronous means "not in parallel".

To me an asynchronous call was always one that effectively ran in parallel with my other code. I know this isn't really true as only one instruction can be computed at a time. But if an asynchronous call is not in parallel, what is code that runs right after another? Why are asynchronous calls derived from a word that means non-parallel if they appear to be even more parallel than normal code?

migrated from programmers.stackexchange.com Jul 24 '15 at 2:26

This question came from our site for professionals, academics, and students working within the systems development life cycle.

  • 3
    I like the blocking vs non blocking terminology to make things clear – Straight Line Jul 23 '15 at 16:34
  • 6
    I'm surprised that you found a definition for "asynchronous" that said, "not in parallel." A more correct definition, especially in computer science, is, "not in series." – Kevin - Reinstate Monica Jul 23 '15 at 20:08
28

The antonym/opposite of "asynchronous" is "synchronous".

In the context of programming, "not in parallel" is a completely wrong definition for "asynchronous". That's just not what it means for us.

In practice, and in most programming languages, what "synchronous" really means is "this will happen right now, before the next line of code." What "asynchronous" really means is "this will happen at some point, but not necessarily right now; the next line of code may happen first." Note that this doesn't have to be something magical built into the language; pushing objects into a queue to get processed later is often considered asynchronous.

For a more precise, technical definition, the one Wikipedia uses on its disambiguation page is pretty good:

In computer programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

Exactly when an "asynchronous" action gets performed depends on the mechanism you're using. In Javascript for instance, a setTimeout(callback, time) call will (roughly speaking) execute callback after time milliseconds pass.

Now, why do we use these terms?

I don't know for sure, but it's worth pointing out that whenever we have parts of our code running asynchronously, we usually have to "synchronize" them back up in order to get predictable behavior at the end. This typically means waiting for some set of async actions to complete. Synchronous code is essentially "synchronized" by default; it's impossible for ordinary statements to get executed out of order unless your compiler/interpeter is horribly broken (or doing clever optimizations). So it might help to think of "asynchronous" as implying "may need to be synchronized later".

9

"Parallel" is an odd definition of synchronous (which is the antonym for asynchronous). Synchronous means "at the same time". Thus asynchronous is "not at the same time".

Whilst no function will return a result at the same time as being called, to the calling code it appears to do so, as the latter's execution stops whilst the function runs. Thus such functions can be seen as synchronous. If a function allows the calling code to continue and then returns a result later, it's no longer synchronous; it's asynchronous.

By allowing the calling code to continue, it can be getting on with other things whilst the function is running; thus the two are parallel.

  • The context of parallel here isn't really odd when you consider the definition of parallel as moving at the same rate or in unison. It's just not a common perception of parallel as not just being "side by side" but at the same time as well. – Joel Etherton Jul 23 '15 at 14:52
5

Syn = Same
Chronous = Time

Synchronous code runs sequentially in the same time line. One piece of code must run before the next piece can run

A = Not
Syn = Same
Chronous = Time

Runs in a different time line, could be sometime later, could be simultaneously on a different core, could be weaved with our thread on the same core. Might run faster and complete earlier than we expect might have to wait for resources and do nothing for a bit. The main point nothing has to wait for it.

Think of two people synchronously walking (or swimming). Their arms and legs move at the same time. Like marching (in parallel normally). One can't (shouldn't) move without the other. They'd be considered "Out of step", if the moved asynchronously there would be no link between their movements.

  • 1
    "Same timeline" is a good way to put it. – user253751 Jul 23 '15 at 22:11
4

In programming synchronous really means deterministic. It doesn't mean all operations happen at the same time, it just means that we know precisely in which order the operations will happen. Think about it as the end of one operation is synchronized with the beginning of the next operation, or that operations are synchronized to the same timeline.

As you say, the word synchronous can (outside of software) have the meaning of "running in parallel", but that would only be in the sense of running simultaneous in lockstep, i.e. deterministically in parallel. But in software parallelism is not deterministic - the processes or threads are not synchronized to each other or to a shared timeline, so execution order across threads is not deterministic. (I suppose that it would be possible to have deterministic parallelism in real-time systems, but I don't know anything about that.)

  • Deterministic is nor entirely fitting. A synchronous function is not necessary deterministic. Deterministic means that given the same inputs, you will always get the same output. You could, for instance have a synchronous random() function which would not be synchronous, because it can return different output given the same input. I agree with the other things you said, I just wanted to point out the subtlety of the word deterministic. – Dan Jul 23 '15 at 23:38
  • Not always. Consider a queue of tasks/operations to perform, and a functionA that runs as a task in such queue, and calls functionB that adds functionC to the queue and returns (so functionA continues). In this case functionB is asynchronous, yet we can know when functionC will be called. This is how many UI toolkits work where there is a single UI thread. – frozenkoi Jul 24 '15 at 0:16
3

To me an asynchronous call was always one that effectively ran in parallel with my other code

That's not strictly correct. Don't confuse threading with synchrony.

Asynchrony is saying to the computer "here's some code but don't run it now, run it after this has happened" (where "this" may be a database call or a time delay). This can be managed in many ways; you don't, for example have to run the waiting code immediately after the delay, you might want to put it in a queue at that point and pop each element off that queue in sequence (thus single-threaded but still asynchronous).

Look at .NET's Task-based Asynchronous Pattern (TAP), which allows you to write a Task in the format "Do this, wait for this, then do something else".

For example:

var parameters = ParseFromXml(input);
var data = await GetDataAsync(parameters);
return ProcessData(data); // This is the asynchronous code here

But the developer of that code doesn't have to make any decisions about threading (is SomethingElse() from one call being done in parallel with DoThis() from another call?), those decisions can all be made by an application/framework specific object called a SynchronisationContext.

This is excellent separation of concerns, but it does lead to confusion because there's an assumption that asynchrony and parallelisation are the same thing.

Partly, this is because we use the word synchrony to mean "the opposite of asynchrony" but that's not correct at all. As others have pointed out, synchrony is two things that exist or occur at the same time, but the opposite of asynchrony (in programming terms) is "run this code, one step after another, blocking all other code."

var parameters = ParseFromXml(input);
var data = GetData(parameters);
return ProcessData(data);

If this is a single-threaded environment, absolutely nothing else can happen while this process runs. In a multi-threaded environment, nothing else can happen in this thread while this process runs, which does allow for other processes in other threads, but there is nothing inherently multi-threaded about synchronous or asynchronous code.

  • I'd say this answer addresses the idea of asynchrony, but it doesn't seem to answer the question of parallelism between synchrony and asynchrony. – Joel Etherton Jul 23 '15 at 14:56
  • @JoelEtherton: What do you think is missing? I'm trying to say that there is no inherent parallelism in synchrony and asynchrony. – pdr Jul 23 '15 at 15:12
  • Yet there is parallelism with synchrony. The term is used in a specific context, and does not follow the traditionally "geometric" viewpoint of parallelism. Given your comment, I don't think this answer is accurate. – Joel Etherton Jul 23 '15 at 15:14
  • @JoelEtherton: I see what you're saying, but I'm struggling to see how to correct the answer. In programming terms, the opposite of asynchronous (which we call synchronous) means "run this code now", which is not necessarily in-parallel with anything else. ie. The concepts of asynchrony (and it's opposite) are as important (if not more so) in a single-threaded context as in a multi-threaded one. – pdr Jul 23 '15 at 15:25
  • Yes, and that's the definition of parallelism that applies. While they are consecutive, they run on the same thread, and thus at the same rate just not at the same time. An asynchronous process would run separately and not be subject to the same timing rules as the original process. This is what breaks the parallelism. Truthfully, it's difficult to describe because of our native visualizations of what it means to be parallel. Keep in mind an asynchronous process may still behave synchronously. – Joel Etherton Jul 23 '15 at 15:31
0

There is no alternative meaning in programming for asynchronous. Not even in "software" programming. It essentially means "non serialised" or in lay terms "not waited for" and a process or event for which the outcome is not depended upon before proceeding to a subsequent task (fire and forget). Confusion may have arisen as the result of a poor explanation of parallel processing (or job scheduling) where ultimately an artificial end point "joins" the completion of the tasks so that we can say they have all completed. This is not the same and relies upon all tasks completing successfully and a time limit creates the effect of a synchronous process. SMS, Javelin launches, and email are other examples of tactically asynchronous methods, the results of which are desirable to know, but we should not be holding our breath.

  • The OP found or derived an alternative meaning. Other answers do not address how this belief may have arisen ? If you zoom out far enough even async processes are blocking in nature, otherwise why are they being executed ? – mckenzm Jul 24 '15 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.