Skip to content

Blocking and non-blocking calls

Category: Foundations · Status: stub · Lessons: chapter 06, Async (planned)

One line: A blocking call does not return until its work is done, holding the caller's thread the whole time; a non-blocking call returns at once and reports that the work is not ready yet or will finish later.

Also called: synchronous and asynchronous calls, blocking I/O, non-blocking I/O.

How it connects

In each language

Rust TcpStream::set_nonblocking makes a read that would wait return an io::ErrorKind::WouldBlock error instead
Go A send or receive on an unbuffered channel ↗ blocks until the other side is ready
C With O_NONBLOCK set, read fails with EAGAIN instead of waiting
Java SelectableChannel.configureBlocking(false) puts an NIO channel in non-blocking mode
Python socket.setblocking(False); in asyncio, asyncio.to_thread runs a blocking function that would otherwise block the event loop
C# Asynchronous file I/O ↗ methods such as ReadAsync do the work without blocking the main thread
JavaScript Atomics.wait blocks, and so cannot be used in the main thread
The operating system The O_NONBLOCK flag of open(2); readiness is then waited for with poll or epoll

Where to read more