Where does `.init_single_threaded` in `std.Io.Threaded` run?

I think .init_single_threaded actually runs on the current thread, but the doc doesn’t say so explicitly and the name does not really help also. Just want to make sure that it does not spawn another thread and run the tasks there.

The name is miss leading, it does not ensure all tasks are run on the main thread. If you want that behaviour, set .single_threaded = true in module options or -fsingle-threaded when using the cli.

async won’t spawn new threads*, but concurrent will, and async tasks may reuse extra threads spawned by concurrent

*async will spawn a new thread in the event a previous concurrent requested one, but could not spawn one.
Though that might be unlikely as most use cases will abort their program when a concurrent call fails? I am not sure. That could also be a bug, or I’m missing something.

1 Like

When a Threaded object is constructed by .init_single_threaded, its Io object actually cannot call concurrent at all. It fails at these lines

const ac_bytes = gpa.alignedAlloc(u8, .of(AsyncClosure), n) catch
    return error.ConcurrencyUnavailable;

since .init_single_threaded has a .failing allocator. So .init_single_threaded actually only operates on the current thread and will not spawn any other thread. Maybe it should be renamed to .this_thread instead.

1 Like

I missed that, thanks.