3

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

2
  • asynchronous != multithreaded != parallelism Commented 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 contribute Commented Nov 14, 2012 at 11:25

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.