Skip to main content

sleep

sleep Function

Syntax:

sleep seconds → nil

Arguments and Values:

seconds—a non-negative real.

Description:

Causes execution to cease and become dormant for approximately the seconds of real time indicated by seconds, whereupon execution is resumed.

Examples:

(sleep 1) → NIL 
;; Actually, since SLEEP is permitted to use approximate timing,
;; this might not always yield true, but it will often enough that
;; we felt it to be a productive example of the intent.
(let ((then (get-universal-time))
(now (progn (sleep 10) (get-universal-time))))
(>= (- now then) 10))
→ true

Side Effects:

Causes processing to pause.

Affected By:

The granularity of the scheduler.

Exceptional Situations:

Should signal an error of type type-error if seconds is not a non-negative real.

Expanded Reference: sleep

Pausing execution

sleep causes the program to pause for approximately the specified number of seconds. It always returns nil.

(sleep 1)
=> NIL

Fractional seconds

The argument can be any non-negative real number, including fractions for sub-second pauses.

(sleep 0.5)
=> NIL

(sleep 1/10)
=> NIL

Measuring sleep duration

You can verify sleep duration using get-internal-real-time.

(let ((start (get-internal-real-time)))
(sleep 0.2)
(let ((elapsed (/ (- (get-internal-real-time) start)
(float internal-time-units-per-second))))
(>= elapsed 0.2)))
;; => T (timing-dependent)

Zero-second sleep

Sleeping for zero seconds is permitted and returns immediately.

(sleep 0)
=> NIL