lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Tue, 31 Oct 2023, at 08:21, Luna wrote:
> Hi all,
> I want give Lua code access to functions implemented by my C program
> that can block while they wait for something to be ready, but the
> program might also want to do other things while it's waiting, and I'd
> rather not make that happen using OS threads. Coroutinies seem like
> they're well suited for that task at first, because when I want to run a
> Lua function I can just wrap it in a thread that can yield first, and
> then the blocking function that the Lua code can call can just yield
> something indicating it's waiting, and the thread can be resumed by the
> program when desired.
> That stops working, though, if the Lua code itself uses coroutines. If
> the blocking function is called within a thread other than the one that
> the main program has created, then when it yields, control will just go
> to another place in the Lua script. I've searched through documentation
> and haven't found a way that I can pause a Lua thread and give back
> control to the program regardless of the state of that thread, even
> though it feels like something like that should be possible.
> Am I missing something?

This is a known issue with Lua coroutines, nested coroutines.

See for example:

Solutions are mediocre. Not because they are no good, but because they rely on every library in your application stack using the same approach, which makes it practically impossible to develop independent libraries that work across the board.

Here's some libraries that implement solutions;


imho it should be dealt with on a Lua level. I actually discussed this with Roberto during the 2022 Lua workshop, and he had some ideas like a "goto" for coroutines. So hopefully we'll see this fixed in the next Lua version.