#How many async threads has Io in flight

1 messages · Page 1 of 1 (latest)

junior walrus
#

Is there an API to know how many async threads Io has in flight? And/or how many are in a specific group?

Basically, I want to make an educated guess if my next async call might actually be called immediately and rather wait in that case, and wait for free thread.

surreal fox
#

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

junior walrus
#

Oh. I'll have to look into that, then.

junior walrus
#

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.

junior walrus
#

Perhaps I could model this by setting async_limit to N - 1, calling async() most of the time, but concurrent() on the special occasions.

surreal fox
#

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.

obsidian fern
#

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.

junior walrus
#

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.

  1. Run X tasks concurrently, but wait for them!
  2. 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?

obsidian fern
#

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.