diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-02-19 16:21:36 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-03-15 10:48:12 -0700 |
| commit | cb094700631ea1ae238ea678c192ce4f85fbecc0 (patch) | |
| tree | b79eb3a4bd26ce880829be3665470c3049d0fe53 /lib/std/Thread | |
| parent | 96d798db8b704a2fd367e58a19d6fe853a8a76a8 (diff) | |
| download | zig-cb094700631ea1ae238ea678c192ce4f85fbecc0.tar.gz zig-cb094700631ea1ae238ea678c192ce4f85fbecc0.zip | |
zig build: add a -j<N> option for limiting concurrency
Diffstat (limited to 'lib/std/Thread')
| -rw-r--r-- | lib/std/Thread/Pool.zig | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/std/Thread/Pool.zig b/lib/std/Thread/Pool.zig index 930befbac5..ed1a4dc052 100644 --- a/lib/std/Thread/Pool.zig +++ b/lib/std/Thread/Pool.zig @@ -17,7 +17,14 @@ const Runnable = struct { const RunProto = *const fn (*Runnable) void; -pub fn init(pool: *Pool, allocator: std.mem.Allocator) !void { +pub const Options = struct { + allocator: std.mem.Allocator, + n_jobs: ?u32 = null, +}; + +pub fn init(pool: *Pool, options: Options) !void { + const allocator = options.allocator; + pool.* = .{ .allocator = allocator, .threads = &[_]std.Thread{}, @@ -27,7 +34,7 @@ pub fn init(pool: *Pool, allocator: std.mem.Allocator) !void { return; } - const thread_count = std.math.max(1, std.Thread.getCpuCount() catch 1); + const thread_count = options.n_jobs orelse @max(1, std.Thread.getCpuCount() catch 1); pool.threads = try allocator.alloc(std.Thread, thread_count); errdefer allocator.free(pool.threads); |
