2 Answers2

3

Non-blocking operations are based on two approaches:

  • by simply returning without data (when no data is available - in such cases the caller has to "come back" by itself and "read" again)
  • by using callbacks. In that context "blocking" means that you wait for an operation to reach a certain state - whereas "non-blocking" means that you trigger the operation - and when that state is reached, you are notified.

Please note: both options do not imply concurrency or multiple threads on the client side. You absolutely can implement such a system using a single process ( think coroutines or node.js for example ).

In that sense: a non-blocking operation is always asynchronous - as you don't know when it will have results for you - or when it will call you back. Both concepts can be be implemented using concurrency, but there is absolute need for doing it that way.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • That's not quite right. If you try to read from a non-blocking socket, you receive nothing and you don't get called back - you would have to do another read later when data become available. – Mark Setchell Aug 01 '17 at 07:02
  • 1
    You are correct - in the strict "socket" sense there are no callbacks. I reworded; hope it is more clear now. – GhostCat Aug 01 '17 at 07:08
  • "Theoretically, you could implement such a system using a single process" but not just in theory: this is the foundation of how Node.js works, for example. – Thomas Aug 01 '17 at 07:49
  • @Thomas Such a coincidence, I was just about to reword my answer to mention node.js ... Seriously: I heard about node.js being singlethreaded, but completely missed that it would be a nice example here. Thanks! – GhostCat Aug 01 '17 at 07:50
1

Non-blocking and concurrent don't really apply to single threaded programs, due to the fact that they refer to ways of managing multiple threads. Non-blocking means that a program doesn't wait for all threads to finish before moving on, and concurrent computation can only happen if you have multiple threads doing the calculation. (Someone please correct me if I'm wrong.)

Asynchrony is the only term that applies to single threaded programming, in the form of human input, communication with other programs, etc. Because of this, no, they don't imply each other in the context of single threaded programs.

Azure Heights
  • 271
  • 2
  • 8
  • 3
    I use non-blocking sockets in my single-threaded programs all the time. Non-blocking simply means that the various function calls (send(), recv(), etc) are guaranteed to always return immediately rather than possibly waiting for an extended period of time (e.g. waiting until some data has been sent or received) – Jeremy Friesner Aug 01 '17 at 05:47
  • Feel free to review the answer that I gave upon reading the question, too ;-) – GhostCat Aug 01 '17 at 06:10