zigar.thread.WorkQueue(ns) - chung-leong/zigar GitHub Wiki
Define a work queue for running functions in multiple worker threads. It accepts a namespace (a
struct, union, enum or opaque) as an argument. Public functions within that namespace
can then be invoked asynchronously with the help of
push().
Functions by the name of onThreadStart and onThreadEnd, if contained in ns, will run at the
start and end of each thread. Arguments to these functions are provided through the options
object given to init(). Their return values
must be void. onThreadStart can return an error as well.
During the queue initialization, zigar.thread.use() is called. During shutdown, zigar.thread.end() is called when the last thread ends.
Usage
const std = @import("std");
const zigar = @import("zigar");
var work_queue: zigar.thread.WorkQueue(worker) = .{};
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
pub fn startup(num_threads: usize) !void {
try work_queue.init(.{
.allocator = allocator,
.n_jobs = num_threads,
});
}
pub fn shutdown(promise: zigar.function.Promise(void)) void {
work_queue.deinitAsync(promise);
}
pub const hello = work_queue.promisify(worker.hello);
pub const world = work_queue.promisify(worker.world);
const worker = struct {
pub fn hello(name: []const u8) void {
std.debug.print("Hello, {s}!\n", .{name});
}
pub fn world() void {
std.debug.print("The world is yours!\n", .{});
}
};
import { hello, world, startup, shutdown } from './work-queue-example-1.zig';
startup(4);
await hello('Tony');
await world();
await shutdown();
Hello, Tony!
The world is yours!
Arguments:
ns:type
Constants:
- Options:
type - ThreadEndParams:
type - ThreadStartError:
type - ThreadStartParams:
type
Functions: