Skip to content
v1.0.0-zig0.15.2

Initialization

How to initialize and configure the Blitz thread pool.

const blitz = @import("blitz");
pub fn main() !void {
// Initialize with automatic thread detection
try blitz.init();
defer blitz.deinit();
// Now use parallel operations...
}
try blitz.initWithConfig(.{
// Number of background worker threads
// Default: CPU count - 1 (main thread also participates)
.background_worker_count = 8,
});
defer blitz.deinit();
// Check if initialized
if (blitz.isInitialized()) {
std.debug.print("Pool is ready\n", .{});
}
// Get worker count (including main thread)
const workers = blitz.numWorkers();
std.debug.print("Running with {} workers\n", .{workers});

Control minimum chunk size for parallel operations:

// Get current grain size (default: 65536)
const current = blitz.getGrainSize();
// Set custom grain size
blitz.setGrainSize(2048);
// Reset to default
blitz.setGrainSize(blitz.DEFAULT_GRAIN_SIZE); // 65536

The default grain size (DEFAULT_GRAIN_SIZE = 65536) works well for most workloads. Reduce it for expensive per-element operations; increase it for trivial operations.

Blitz does not auto-initialize. You must call init() before using parallel operations:

// Correct usage
try blitz.init();
defer blitz.deinit();
const sum = blitz.iter(i64, data).sum();

Calling initWithConfig() on an already-initialized pool returns error.AlreadyInitialized.

// Get execution stats
const stats = blitz.getStats();
std.debug.print("Executed: {}, Stolen: {}\n", .{ stats.executed, stats.stolen });
// Reset stats
blitz.resetStats();
Workload TypeRecommended Threads
CPU-bound (compute)CPU count
Memory-bound (copy, scan)CPU count / 2
MixedCPU count - 1
I/O-boundNot ideal for Blitz
pub fn initBlitz() !void {
const cpu_count = std.Thread.getCpuCount() catch 4;
try blitz.initWithConfig(.{
// Leave 1 core for OS and other tasks
.background_worker_count = @max(1, cpu_count - 2),
});
}
pub fn main() !void {
try initBlitz();
defer blitz.deinit();
// Application code...
}