lua-users home
lua-l archive

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

I tried the following, with sockets being the best so far but it appeared to hang sometimes when running in the background.

the system call seems to hinder Ctrl-C.

Quotes from

Solution: Busy Wait

local clock = os.clock
function sleep(n)  -- seconds
  local t0 = clock()
  while clock() - t0 <= n do end
-- warning: clock can eventually wrap around for sufficiently large n
-- (whose value is platform dependent).  Even for n == 1, clock() - t0
-- might become negative on the second that clock wraps.

Solution: sleep command

function sleep(n)
  os.execute("sleep " .. tonumber(n))
Solution: select()
require "socket"

function sleep(sec), nil, sec)


On 10/13/10 7:42 PM, Del Buono, Matthew Paul wrote:
I think you missed the "non busy" part.

There's no built in sleep but it should be trivial to make a binding for it. I've also seen people use select() from luasocket for this purpose.

Sent from my Verizon Wireless BlackBerry

From: Gabriel Duarte <>
Date: Wed, 13 Oct 2010 14:26:26 -0300
To: Lua mailing list<>
ReplyTo: Lua mailing list <>
Subject: Re: Sleep

Try out

function sleep(length)
    local start = os.clock()
    while os.clock() - start < length do end

On Wed, Oct 13, 2010 at 2:20 PM, Henning Diedrich <> wrote:
Hi list,

is there a sleep function that is
- non-busy
- allows for ctrl-C
- doesn't get stuck
- does not require a C extension

I tried busy, system call and sockets from for the above reasons they don't work for me.

Is there a rock offering a simple C-programmed sleep?

It's for a very simple game world tick with the focus on demonstrating (other) principles, and ease of getting to run oneself.


Gabriel Duarte
Linux User #471185
Rio de Janeiro - RJ

(55) (21) 9463-7760  -> Mobile
(55) (21) 2464-9302  -> Home
(55) (21) 2529-5080  -> Work