Does Lua have a 'sleep' function
Hi! I just started with Lua and man am I impressed! I integrated it with my engine in only 2 hours and everything is working perfectly.
I went through the Lua wiki tutorials but can't seem to find if there is a built in 'sleep' function like in C/C++. If there isn't, how would one write a alternative? Thanks
Well, for simple "pause for x number of seconds" behavior, you could use the sleep function in the lposix library here. It's basically just a wrapper around POSIX functions such as sleep.
I was looking for a sleep function too in Lua but didnt find any.
Turned out I needed the LuaSocket library later on and there it was!
From LuaSocket reference:
Turned out I needed the LuaSocket library later on and there it was!
From LuaSocket reference:
socket.sleep(time) Freezes the program execution during a given amount of time.
I think that when most people ask about a sleep function in Lua, they probably really want something like 'yield' in a Lua coroutine (much like a thread in other languages) and returns you to the language you called it from, waiting for you to call it again later. I'm not sure what effect simply calling a sleep function from Lua would achieve in relation to the rest of the program it is embedded in; I would expect the whole thing would pause, which is rarely what people want.
Would this be ok?
-- os.clock() returns an approximation of the amount of CPU
-- time used by the program, in seconds
function sleep(seconds)
time = os.clock()
while os.clock()-time < seconds do end
end
-- call sleep to sleep for 200 ms
sleep(0.2)
-- os.clock() returns an approximation of the amount of CPU
-- time used by the program, in seconds
function sleep(seconds)
time = os.clock()
while os.clock()-time < seconds do end
end
-- call sleep to sleep for 200 ms
sleep(0.2)
No. That is not ok. That's called "busy-waiting", and is the programming equivalent of burping into someone else's dinner. It will use 100% of CPU time, spinning wildly, until the wait is over. The sleep() function is in C for a reason: It allows you to wait without taking up all the computer's time doing it.
Ok. I handled it. I create a function in C++ which, when called from Lua, pauses Lua's thread with lua_yield and starts a count-down. When the countdown reaches 0 I resume Lua's thread with lua_resume. Maybe not very optimal if you need to sleep for very small intervals but in other cases works perfectly and there is no 100% CPU usage!
Quote: Original post by Sneftel
is the programming equivalent of burping into someone else's dinner.
Thanks, ive been looking for an appropriate way to tell people how bad busy waiting is, that works nicely.
Quote: Original post by Krun
Ok. I handled it. I create a function in C++ which, when called from Lua, pauses Lua's thread with lua_yield and starts a count-down. When the countdown reaches 0 I resume Lua's thread with lua_resume. Maybe not very optimal if you need to sleep for very small intervals but in other cases works perfectly and there is no 100% CPU usage!
Krun,
That's exactly how I've done it. I have a module that is the lua dispatcher. When new lua_state objects are created, they get wrapped in an object and stored in the dispatcher. Whenever a lua script calls 'sleep' it yields the script and sets the timer. The dispatcher moves on to the next script, checks if it's timer has expired, and if it has, resumes it. It works out nicely.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement