Urgent.News

the world's headlines, one feed

Tech

Zig's Io.Threaded is Neat

Comments

Zig's std.Io.Threaded is a neat implementation of the Io interface that offers concurrency through blocking syscalls. It features deterministic parallelism, where you split the problem into independent partitions and implement a function to process each partition. Concurrency also involves cancellation, which is crucial when one asynchronous computation is no longer needed.

Zig's implementation of cancelation is more practical than others, as it allows you to cancel syscalls reliably. This is achieved by signaling a thread, which sets a flag in shared memory to request cancellation, and then signals the canceling thread until acknowledgment.

On POSIX, this is done by delivering signals to the thread, which wakes it up and returns EINTR from the blocked syscall. However, signals are inherently racy, so there's a protocol in place to ensure reliable cancelation. On Windows, there's a more direct NtCancelSynchronousIoFile mechanism. Zig's approach effectively handles concurrency between fibers, IO Completion Ports, Job objects, and more, providing a comprehensive concurrency story.

This is in contrast to Java's thread interruption mechanism, which doesn't support interrupting syscalls. In summary, Zig's std.Io.Threaded takes a unique approach to concurrency, separating "may run concurrently" from "must run concurrently" at the interface level, resulting in more precise signatures and easier understanding of the underlying processes.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written; read the original for the full account.

Read the original at matklad.github.io →

More in Tech