lua-users home
lua-l archive

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


On 7/9/2012 2:19 PM, Laurent FAILLIE wrote:
Thanks for the explanation, I made it working. I have a window with both
a background counter and [+]  and [Reset] button that interact with it :)
It's incredible how fast is Lua/Lgi learning curve :D

Glad to hear that :-)

 > Note Lua's coroutines
 > can be really helpful here, because you can make
ImDoingSomeHardWork() running as coroutine, spiced with
coroutine.yield() at 'strategic' places.  Then you
 > pass coroutine instance (instead of function) to GLib.idle_add(), and
voila, coroutine is periodically resumed when GLib's mainloop has
nothing else to do.

I didn't tried yet co-routines. But when you said "resumed", it means
the co-routine will restart from where it was interrupted and I don't
have to manage context or such. I mean like an OS task is interrupted by
an IRQ ?

Yes.

I'll have to learn also how to interact with the co-routine. Signal ?
Semaphore ???

Coroutine gives up processing by calling coroutine.yield() in its body. All interaction with coroutine basically happens by resuming it by coroutine.resume(co) call, which causes coroutine continue running as if coroutine.yield() returns. This is standard Lua stuff, look it up in the Lua manual.

lgi's support for coroutines is just that when coroutine is passed where any kind of callback is expected, lgi resumes the coroutine when the callback is about to be invoked. See e.g. samples/giostream.lua or samples/gtk-demo/demo-images.lua for slightly more involved example doing this.

bye,
pavel