[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How to swap coroutine ?
- From: Gaspard Bucher <gaspard@...>
- Date: Tue, 13 Sep 2011 09:47:03 +0200
On Tue, Sep 13, 2011 at 8:53 AM, Graham Wakefield
<wakefield@mat.ucsb.edu> wrote:
Exactly. Start your script with lua_resume() rather than lua_call().
On Sep 12, 2011, at 1:36 PM, Wesley Smith wrote:
>> I know it is not that hard to add a "scheduler:run()" at the end of a main
>> script, but I would like the main thread to give up control before reaching
>> the end of the script:
>
> If you run the entire script as the body of a coroutine, it can yield
> at any point.
> wes
>
This is a good idea if you run a custom executable, which I don't (I want to use plain Lua).Technically, what I want to do doesn't seem impossible (it's some kind of resumable goto)
and it would enable to completely hide the scheduling stuff. For example, I wrap
coroutine.yield(timeout) as "sleep(timeout)" which is very easy to grok and it worked fine
as long as I used real OS threads. If you do the same with coroutines you get errors. For
a total beginner, code "A" and "B" are the same, but "B" has some boilerplate code that
I would *love* to hide:
-- ========== A
play('C#')
sleep(500) -- error
play('E')
sleep(500)
-- ========== B
sched:thread(function()
play('C#')
sleep(500) -- ok
play('E')
sleep(500)
end)
sched:run()
--
If there is no solution apart from altering the bytecode (a job for Peter Cawley...),
I can live with "B".