[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Strategies for Lua coroutines in C programs?
- From: Sean Conner <sean@...>
- Date: Fri, 10 Apr 2015 03:03:33 -0400
It was thus said that the Great Marc Balmer once stated:
> In an application that is written in C I am already using Lua for
> scripting, now I want to perform certain task in parallel to the main
> function (a cash register), e.g. handling certain credit card
> transactions in the background.
> My first approach was to create the coroutines using lua_newthread() and
> keep a list of runnable coroutines. Then in the main event loop (it's
> an X11 application), when there are no X events to process, I would
> check if there are runnable coroutines and resume them, if there are.
> This immediately led to my application using 100% CPU time ( because
> it's constantly calling into Lua).
> My second approach, which seems to work well for now, was to return a
> number in coroutine.yield(), which is interpreted by the C program as a
> interval value in milliseconds. The C program would then create an
> XtInterval and schedule the coroutine to be resumed in the specified
> time. This way a coroutine schedules itself.
> What other strategies are being used? How do others handle coroutines
> in hosted programs?
At work, I wrote a network service that uses coroutines to handle the
actual requests. Since I'm not concerned about long running coroutines
(because I wrote the code, and I know there aren't any) I have the
coroutines yield at any potential blocking point (like making a DNS
request). Check the bottom of this post
for some more details. It's probably similar to yours, but the time based
rescheduling logic exists to timeout network requests.