🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Does Lua have a 'sleep' function

Started by
8 comments, last by Krun 20 years ago
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
Advertisement
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:
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)
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.
I've been wanting to write something like your dispatcher but don't know which functions to use. Please have a look at my other thread titled "Load scripts without running them". Thanks

This topic is closed to new replies.

Advertisement