Skip to content

zigar.thread.use() ᴾᴴᴾ

Chung Leong edited this page Jul 18, 2026 · 2 revisions

JavaScript | PHP


Enable calling of PHP functions from other threads. The 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.

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();
}
<?php

require __DIR__ . '/vendor/autoload.php';

use Revolt\EventLoop;

ini_set('zigar.event_loop', 'revolt');

EventLoop::defer(function() {
    $m = zigar_use(__DIR__ . '/thread-example-1.zig');
    $m->startup();
    try {
        $m->spawn(function() {
            echo "Hello world\n";
        });
        // wait two seconds
        $suspension = EventLoop::getSuspension();
        EventLoop::delay(2, function() use($suspension) {
            $suspension->resume();
        });
        $suspension->suspend();
    } finally {
        $m->shutdown();
    }   
});
EventLoop::run();
Hello world

Return value:

error{UnableToUseThread}!void

Clone this wiki locally