Another weekend, another weekend read, this time all about Resource efficient Thread Pools with Zig
My book Thinking in Distributed Systems is now available. 12 chapters, full of mental models, diagrams, illustrations, and examples, all about distributed systems.
In his blog post Resource efficient Thread Pools with Zig, King Protty discusses the result of a two-year effort: A resource efficient implementation of thread pools in Zig.
A thread pool is a set of threads to which work, often called tasks, can be dispatched. A thread pool amortizes the costs associated with creating and destroying threads, which can be significant compared to the tasks being performed. Additionally, thread pools prevent tasks from blocking each other by having other threads ready for processing.
var stack: ?*Task = null;
pub fn schedule(task: *Task) void {
task.next = stack;
stack = task;
}
pub fn run() void {
while (stack) |task| {
stack = task.next;
(task.callback)(task);
}
}
King’s post takes the reader on a detailed journey through the development process. He starts with the basics of thread pools and gradually builds up to more complex concepts, ensuring a comprehensive understanding of the underlying mechanisms. Along the way, Protty explains the rationale behind each design choice.
In addition, the blog post covers various advanced topics such as lock-free algorithms, intrusive memory, and work-stealing mechanisms, all aimed at enhancing resource efficiency.
A truly outstanding Weekend Read
Happy Reading
Resource efficient Thread Pools
King Protty
For those unaware, a thread pool is just a group of threads that work can be dispatched to. Having a group amortizes the costs of creating and shutting down threads which can be expensive in comparison to the work being performed. It also prevents a task from blocking another by having other thread ready to process it. Thread pools are used everywhere from your favorite I/O event loop (Golang, Tokio, Akka, Node.js), to game logic or simulation processing (OpenMP, Intel TBB, Rayon, Bevy), and even broader applications (Linkers, Machine Learning, and more). It's a pretty well explored abstraction, but there's still more room for improvement.