Skip to content

zigar.thread.WorkQueue(ns).waitAsync(self, promise)

Chung Leong edited this page Oct 21, 2025 · 4 revisions

Resolve the given promise when all threads have started and called ns.onThreadStart() (if present). Reject the promise if any error is encountered any of the threads.

Usage

const std = @import("std");
const zigar = @import("zigar");

const WorkQueue = zigar.thread.WorkQueue(ns);
var work_queue: WorkQueue = .{};
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();

pub fn startup(num_threads: usize, promise: zigar.function.PromiseOf(WorkQueue.waitAsync)) !void {
    try work_queue.init(.{
        .allocator = allocator,
        .n_jobs = num_threads,
        .thread_start_params = .{"/tmp/no-where"},
    });
    work_queue.waitAsync(promise);
}

const ns = struct {
    threadlocal var file: ?std.fs.File = null;

    pub fn onThreadStart(path: []const u8) !void {
        file = try std.fs.openFileAbsolute(path, .{});
    }
};
import { startup } from './work-queue-example-5.zig';

await startup(4);
[Error: File not found] { number: 51 }

Arguments:

  • self: @This()
  • promise: Promise(void) or Promise(ThreadStartError!void)

Return value:

void


WorkQueue(ns) | Promise(T) | ThreadStartError

Clone this wiki locally