# How to "reserve first" units of concurrency

**URL:** <https://ziggit.dev/t/how-to-reserve-first-units-of-concurrency/18501>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [November 17, 2026, 10:57pm UTC](https://ziggit.dev/t/how-to-reserve-first-units-of-concurrency/18501 "2026-11-17T22:57:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [November 17, 2026, 10:57pm UTC](https://ziggit.dev/t/how-to-reserve-first-units-of-concurrency/18501/1 "2026-11-17T22:57:54Z")

</div>

The following test fails on 0.16.0, 0.17.1, and master:

```zig
const std = @import("std");

fn noop() void {}

test {
    var threaded: std.Io.Threaded = .init(std.testing.allocator, .{
        .concurrent_limit = .limited(1),
    });
    defer threaded.deinit();
    const io = threaded.io();

    for (0..10_000) |i| {
        var f = io.concurrent(noop, .{}) catch |err| switch (err) {
            error.ConcurrencyUnavailable => |e| {
                std.log.err("failed at i: {}", .{i});
                return e;
            },
        };
        defer f.cancel(io);

        f.await(io);
    }
}

```

```zig
[default] (err): failed at i: 967
1/1 test2.test_0...FAIL (ConcurrencyUnavailable)
zig/0.18.0-dev.1180+2e9bb03df/lib/std/Io/Threaded.zig:2198:9: 0x11d79fe in concurrent (std.zig)
        return error.ConcurrencyUnavailable;
        ^
zig/0.18.0-dev.1180+2e9bb03df/lib/std/Io.zig:2620:25: 0x124c76f in concurrent__func_1011 (std.zig)
    future.any_future = try io.vtable.concurrent(
                        ^
test2.zig:16:17: 0x124c54c in test_0 (test2.zig)
                return e;
                ^
0 passed; 0 skipped; 1 failed.
1 errors were logged.
error: the following test command failed with exit code 1:
.zig-cache/o/da7898f856eaa4da1db1a72e88d5c3bc/test --seed=0xfd24701

```

How do I reserve units of concurrency before I use them so I can ensure concurrency is never unavailable?

My use case is that I have an HTTP client that is repeatedly making requests to a database and I would like that client to never experience `ConcurrencyUnavailable`. I have already gone to great lengths reserve all its memory too… in the interest of TigerStyle.  
I need timeouts on the HTTP request so I am using `Io.Select` with a concurrent `client.fetch` and concurrent `sleep`.

The only thing I can really do right now is set my concurrency limit extra high.

---

<div class="post-metadata">

**Author:** ![pasta](https://ziggit.dev/user_avatar/ziggit.dev/pasta/32/6273_2.png) [@pasta](https://ziggit.dev/u/pasta)\
**Post date:** [November 18, 2026, 12:20am UTC](https://ziggit.dev/t/how-to-reserve-first-units-of-concurrency/18501/2 "2026-11-18T00:20:36Z")

</div>

Why not start concurrent tasks that immediately wait on a [Semaphore](https://ziglang.org/documentation/master/std/#std.Io.Semaphore), blocking until tasks are ready? Basically implementing a job/event queue.

---

<div class="post-metadata">

**Author:** ![KilianHanich](https://ziggit.dev/user_avatar/ziggit.dev/kilianhanich/32/8254_2.png) [@KilianHanich](https://ziggit.dev/u/KilianHanich)\
**Post date:** [November 18, 2026, 1:52am UTC](https://ziggit.dev/t/how-to-reserve-first-units-of-concurrency/18501/3 "2026-11-18T01:52:45Z")

</div>

There are a few ways you could do this, I guess.

```Zig
fn work(io: std.Io, queue: *std.Io.Queue(Item)) void {
    while (queue.getOne()) |item| {
        // handle item
        // you can use Select in here
    } else |err| {
        std.log.info("received {t} from queue", .{err});
    }
}

pub fn main(init: std.process.Init) !void {
    var queue_buffer: [8]Item = undefined;
    var queue: std.Io.Queue(Item) = .init(&queue_buffer);
    var group: std.Io.Group = .init;
    defer group.await(init.io);
    errdefer group.cancel(init.io);
    for (0..std.Thread.getCpuCount()) |_| { // there might be a want to be able to request this in general from std.Io since this assumes threads
        try group.concurrent(init.io, work, .{init.io, &queue});
    }
    defer queue.close(init.io);
    while (...) {
        ... generate item
        queue.putOneUncancelable(init.io, item);
    }
}

```

another possibility:

```Zig
fn work(io: std.Io, sema: *std.Io.Semaphore, item: Item) void {
    defer sema.post(io);
    // handle item
}

pub fn main(init: std.process.Init) !void {
    var sema: std.Io.Semaphore = .{
        .permits = std.Thread.getCpuCount(), // see comment above
    };
    while (...) {
        try sema.wait(init.io);
        ... generate item
        var f = init.io.concurrent(work, .{io, &sema, item}) catch |err| switch (err) {
            ...
        };
        defer f.cancel(init.io);
        f.await(io);
    }
}

```
