Skip to content

Instantly share code, notes, and snippets.

@benwaffle
Created December 31, 2021 05:57
Show Gist options
  • Save benwaffle/831777ac5c8242e4dad5b43fbe5684b0 to your computer and use it in GitHub Desktop.
Save benwaffle/831777ac5c8242e4dad5b43fbe5684b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env vala --pkg gio-2.0
delegate void Func();
struct Box {
Func f;
}
async void run (Func f) {
var dummy = new Object ();
var task = new Task (dummy, null, () => Idle.add (run.callback));
Box boxed = { f };
task.set_task_data ((void*)&boxed, null);
task.run_in_thread ((task, obj, data, cancel) => {
Box *b = (Box*) data;
b.f ();
task.return_pointer (null, null);
});
yield;
}
void main() {
var loop = new GLib.MainLoop ();
print ("start\n");
int i = 3;
run.begin (() => print (@"running - $i\n"), (obj, res) => {
run.end (res);
print ("done\n");
loop.quit ();
});
loop.run ();
print ("end\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment