Or can we have asynchronous code that executes in the same thread.
-
asynchronous != multithreaded != parallelismCameron MacFarland– Cameron MacFarland2012-11-13 12:17:33 +00:00Commented Nov 13, 2012 at 12:17
-
that is obvious. My question was different and if you have any real answer for it feel free to contributeAdrian Zanescu– Adrian Zanescu2012-11-14 11:25:01 +00:00Commented Nov 14, 2012 at 11:25
3 Answers
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:
Comments
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.
Comments
Async is common is JavaScript (used for web services, for example), but almost all JavaScript until now has been single threaded.