Adrian Zanescu
Adrian Zanescu

Reputation: 8008

Does async necessary involves multithreading or parallelism?

Or can we have asynchronous code that executes in the same thread.

Upvotes: 3

Views: 157

Answers (3)

bnieland
bnieland

Reputation: 6506

Async is common is JavaScript (used for web services, for example), but almost all JavaScript until now has been single threaded.

Upvotes: 0

Randolpho
Randolpho

Reputation: 56391

Actually, yes, you can have async code that executes in the same thread. Most basic IO these days is actually asynchronous; reads and writes are requested and processed outside the CPU, when they are complete a flag is set and then the program can address the values. The program does this by occasionally checking the value of the flag during it's normal processing and responding when the value indicates availability. The operating system will typically coordinate this for higher-level programs.

That's a really dumbed down version of the truth, but it's correct enough for this discussion. For more reading, I suggest you start here:

http://en.wikipedia.org/wiki/Asynchronous_I/O

Upvotes: 4

You can definitely write code which would be asynchronous, but single threaded. An example might be something with a bunch of sockets open, which uses select and non-blocking IO to write small, short things for different "sessions" and breaks these into chunks. This could definitely be asynchronous, depending on quite what was being sent and how it was controlled.

You could do things without networking too, but that's probably the most trivial example.

Upvotes: 2

Related Questions