lua-users home
lua-l archive

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


On Fri, 2005-02-04 at 11:01, PA wrote:
> Hello,
> 
> I'm playing with coroutines and I wrote a little timer of sort:
> 
> http://dev.alt.textdrive.com/file/lu/LUTimer.lua
> 
> This works alright as far as the timer itself goes, but unfortunately 
> the dispatcher loop consume an inordinate amount of CPU looping thought 
> all the tasks... :/


> I'm not quite sure how to deal with the "busy wait" introduced by the 
> dispatcher run loop though. Ideally the dispatching should be based on 
> some kind of events. But at first glance I don't quite see how to pull 
> this out using only Lua itself... ideas much appreciated :)

The right way to do this requires pthreads and an OS alarm.

The dumb but easy way to do this, is to emulate the OS alarm

Roughly the timer coroutine is:

	check time, return if clock has done a tick
	otherise go to sleep for a while
	yield to the scheduler
	repeat above stuff

Make sure to check the time *before* sleeping in case
the alarm has already gone off.



-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net