zigar.thread.use() - chung-leong/zigar GitHub Wiki
Enable calling of JavaScript functions from other threads. Node.js event loop will continue
to await call requests until end()
is called.
use()
can be called multiple times, with only the first call having actual effect. An internal
counter ensures that call handling is terminated only after end()
has been called a matching
number of times.
It does nothing when the target platform is WebAssembly.
Usage
const std = @import("std");
const zigar = @import("zigar");
pub fn startup() !void {
try zigar.thread.use();
}
pub fn spawn(cb: *const fn () void) !void {
const thread = try std.Thread.spawn(.{}, run, .{cb});
thread.detach();
}
fn run(cb: *const fn () void) void {
cb();
}
pub fn shutdown() void {
zigar.thread.end();
}
import { spawn, startup, shutdown } from './thread-example-1.zig';
startup();
spawn(() => {
console.log('Hello world');
});
await new Promise(r => setTimeout(r, 200));
shutdown();
Hello world
Return value:
error{UnableToUseThread}!void