mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 22:35:24 +00:00
std.Thread.WaitGroup: add spawnManaged
Provides a convenient way to spawn a new thread that bypasses a thread pool. Appropriate when the spawned thread delegates all of its work.
This commit is contained in:
parent
ffd53a459e
commit
3efdfe612e
@ -1,3 +1,4 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const WaitGroup = @This();
|
||||
@ -43,3 +44,24 @@ pub fn isDone(wg: *WaitGroup) bool {
|
||||
|
||||
return (state / one_pending) == 0;
|
||||
}
|
||||
|
||||
// Spawns a new thread for the task. This is appropriate when the callee
|
||||
// delegates all work.
|
||||
pub fn spawnManager(
|
||||
wg: *WaitGroup,
|
||||
comptime func: anytype,
|
||||
args: anytype,
|
||||
) void {
|
||||
if (builtin.single_threaded) {
|
||||
@call(.auto, func, args);
|
||||
return;
|
||||
}
|
||||
const Manager = struct {
|
||||
fn run(wg_inner: *WaitGroup, args_inner: @TypeOf(args)) void {
|
||||
defer wg_inner.finish();
|
||||
@call(.auto, func, args_inner);
|
||||
}
|
||||
};
|
||||
wg.start();
|
||||
_ = std.Thread.spawn(.{}, Manager.run, .{ wg, args }) catch Manager.run(wg, args);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user