In computer networking – and in a lot of other fields actually – I hear a lot of reference to the terms “blocking”, “non-blocking”, “synchronous”, and “asynchronous”. I was wondering if anyone could explain in pretty straightforward/simple terms what these are supposed to mean?
Share
A “blocking” call “blocks” the program that calls it until it completes. Your program has to wait for it to do (whatever) before the next statement runs. Most function calls are “blocking,” for example,
set x to 4 + 4will not go on to the next statement until it computes the value of8and assigns it tox. Likewise, a blocking or synchronous network method will hold up the calling program until it completes. In the case of something like “send a packet to a remote system,” this time may be measurable in seconds, or longer, instead of the microseconds (or less) that arithmetic consumes.A “non-blocking” or asynchronous method usually, instead, either deposits its results in a “mailbox” or “queue” of some kind, or (more commonly) will call back a function that you provide when it completes. This is often/usually better for a program that does anything else (for example, displaying a user interface) while it’s waiting on a relatively slow network process to complete.
When accessing relatively fast local services, like local disc I/O, inter-process communications on one computer, or sending output to a local display, blocking I/O is sometimes preferred because it’s easier to write.
Example of blocking network I/O:
versus:
Asynchronous I/O is almost always used at the application level for GUI programming, as well. For example, a terminal (stream)-based program might be synchronous awaiting for user input, while a GUI program might be asynchronous, allowing you to randomly choose various controls or perform other actions (like resizing a window) that require it to accept messages of various times through either callback methods or event handlers, which more-or-less amount to the same type of thing as the network callback example, above.