Thread pool
Thread pools address two different problems:
- They usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and
- They provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks.
In this example, we spawn 10 tasks into thread pool, and use WaitGroup
to wait for them to finish.
const std = @import("std");
const print = std.debug.print;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer if (gpa.deinit() != .ok) @panic("leak");
const allocator = gpa.allocator();
var pool: std.Thread.Pool = undefined;
try pool.init(.{
.allocator = allocator,
.n_jobs = 4,
});
defer pool.deinit();
var wg: std.Thread.WaitGroup = .{};
for (0..10) |i| {
pool.spawnWg(&wg, struct {
fn run(id: usize) void {
print("I'm from {d}\n", .{id});
}
}.run, .{i});
}
wg.wait();
print("All threads exit.\n", .{});
}