#How many async threads has Io in flight
1 messages · Page 1 of 1 (latest)
You can't really depend on it. It seems like you need a thread pool. You can build one using io.concurrent and Io.Queue
Oh. I'll have to look into that, then.
The documention makes it sound as if Io is a thread pool: /// Maximum thread pool size (excluding main thread) when dispatching async tasks.
Basically want I imagined is: If I know I have N threads, most of the ime I want async to use a thread if one of N -1 threads happens to be free, otherwise just run on main thread. But sometimes I want to use that last thread that I know I still have, for something that probably will take longer than any of the things already in flight.
Perhaps I could model this by setting async_limit to N - 1, calling async() most of the time, but concurrent() on the special occasions.
Io is a thread pool, but a weird one, because you can't know when is async going to run inline. There is an issue to make it work more like a proper thread pool.
https://codeberg.org/ziglang/zig/issues/30595
But i seriously think if your need is to run a number of tasks concurrently, just use concurrent and queue for that.
Currently, Zig code that makes extensive use of async combined with std.Io.Threaded is subject to suboptimal performance when a long-running task is executed while all CPU cores are busy. When his happens, the long-running task is not queued but rather executed directly in the async call. T...
Io is a very opaque concurrency pool. It may use threads, it may use green threads, it may use something else entirely or a combination of any of them.
It is deliberately opaque over those details to allow the implementation more freedom.
At the moment I have more or less 2 use cases, and code that handles them. I was just spooked by the change to futex API.
- Run X tasks concurrently, but wait for them!
- Launch a task async and move on, I might launch more tasks async, but I definitely wait for them before doing something like 1. again. (And I actually also have a fixed number of async threads and will run inline if none is free. So in that regards it matches quite well with what I understand from IO)
Where can I educate myself about concurrent and queue?
Is concurrent just the function? What do I need the queue for?
yes it is just a function, that runs a provided function concurrently.
you'd want to run them through an Io.Group, which doesnt let you return values so you need an alternate way to get them, thats what the queue is for.
using a group lets you wait on or cancel all the tasks run through it at once.
if you need to wait on/cancel them idividually then you wont be able to use a group.
it just simplifies managing tasks when applicable.