Zig's Io.Threaded: Canceling Blocking Syscalls with Signals
Zig's Io.Threaded Is Neat
Zig's new Io interface offers a 'boring' thread-based implementation that nonetheless achieves something remarkable: full cancellation of blocking syscalls. By leveraging signals and a shared-memory flag protocol, it interrupts reads and writes on POSIX, while Windows uses the more direct NtCancelSynchronousIoFile. The author, who had long wanted this, explains how it works, contrasts it with Java's interruption and pthread_cancel, and highlights Zig's separation of async and concurrent interfaces.
It’s easy enough, in any loopy code, to do something like while (true) { if (is_canceled()) return error.Canceld; ... } But, the thread is instead blocked inside the syscall in the kernel, programming language APIs generally doesn’t give any way to unblock it.
- _old_dude_
Just here to say that Java as interruptible channels since the beginning of 2000.
You can interrupt a blocking IO operation with either interrupt() or close().
https://docs.oracle.com/en/java/javase/25/docs/api/java.base...
- lll-o-lll
All my days of low level stuff were in the windows world where async/cancel was supported since nt kernel days at least. Wrapping this coherently at some level of abstraction was always worth doing, although threads are just one way. Overlapped I/O let you do it all on one thread. Linux looks harder.
Great that Zig is supporting this more “first class”; not strictly required, but looks useful.
- two_handfuls
Oh man this finishes too early, I would want to read more!
- phplovesong
I know syntax does not REALLY matter, but Zig is just kind of subpar syntax wise. Its really verbose and picked weirdly from what already exists. Zig code is usually hard to read because it really noicy, and has weird things not existing in other languages without merit.
- zuzululu
I'm shipping in Rust but any reason to do so in Zig ? What does it offer over Rust ? Something tangible benefit to end user or developer using ai assisted development ?