开发者

Does async necessary involves multithreading or parallelism?

开发者_C百科

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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜