Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are we able to know a IO is blocking or non-blocking

Tags:

c#

Reading C# documentation, I wonder how one can know if the IO is blocking or not. For example the BinaryWriter.Write method, I cannot find any information on the documentation on whether the method is blocking.

like image 608
Tay Wee Wen Avatar asked Jul 24 '14 13:07

Tay Wee Wen


People also ask

How can you tell if a socket is blocking or non-blocking?

From MSDN, the return value of connect(): On a blocking socket, the return value indicates success or failure of the connection attempt. With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR , and WSAGetLastError() will return WSAEWOULDBLOCK.

What is the difference between blocking and non blocking I O model?

Blocking vs. Java IO's various streams are blocking. It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written. Non blocking IO does not wait for the data to be read or write before returning.

What allows non blocking IO?

The event loop is what allows Node. js to perform non-blocking I/O operations despite the fact that JavaScript is single-threaded. The loop, which runs on the same thread as the JavaScript code, grabs a task from the code and executes it.

What is blocking and non-blocking mode?

"Blocking" simply means a function will wait until a certain event happens. "Non blocking" means the function will never wait for something to happen, it will just return straight away, and wait until later to complete the action.


1 Answers

BinaryWriter.Write only returns when its work is completed. That makes it blocking. The waiting time is potentially unbounded.

Asynchronous methods are usually easy to spot because they return Task or IAsyncResult (which corresponds to the old APM pattern). They also are named appropriately (BeginXxx or XxxAsync). When such a method returns you do not have the result yet. That indicates to you that the computation is still unfinished.

The docs call this property out for async methods. Because almost all methods are blocking and synchronous (e.g. all string methods) this behavior not noted in the docs. It is the default.

like image 157
usr Avatar answered Sep 20 '22 02:09

usr