with-lock-heldPortable Threadswithout-lock-heldwith-timeoutGoTo Top

with-timeout   (seconds timeout-form*) form* => result*[Macro]

Purpose
Bound the time allowed to evaluate forms to seconds, evaluating timeout-forms if the time limit is reached.

Package   :portable-threads

Module   :portable-threads

Arguments and values

seconds     A number
timeout-forms     An implicit progn of forms to be evaluated if the timed forms do not complete before seconds seconds have elapsed
forms     An implicit progn of forms to be evaluated
results     The values returned by evaluating the last form or the last timeout-form

Returns
The values returned by evaluating the last form if completed in less than seconds seconds; otherwise the values returned by evaluating the last timeout-form

Errors
Threads (multiprocessing) is not supported on the Common Lisp implementation. However, with-timeout is also supported on non-threaded SBCL.

Description
If the evaluation of forms does not complete within seconds seconds, execution of forms is terminated and the timeout-forms are evaluated, returning the result of the last timeout-form. The timeout-forms are not evaluated if the forms complete within seconds seconds, in which case the result of the last form is returned.

See also
    condition-variable-wait-with-timeout

Examples
Evaluate a simple form, with a one-second time out:

  > (with-timeout (1 ':timed-out) 
       ':did-not-time-out)
  :did-not-time-out
  >
Again, but this time sleep for two seconds to cause a time out:
  > (with-timeout (1 ':timed-out)
       (sleep 2) 
       ':did-not-time-out)
  :timed-out              ; (after 1 second)
  >
Uses of with-timeout can be nested:
  > (with-timeout (1 ':timed-out-outer)
       (with-timeout (2 ':timed-out-inner)
          (sleep 3)
          ':did-not-time-out))
  :timed-out-outer        ; (after 1 second)
  > (with-timeout (2 ':timed-out-outer)
       (with-timeout (1 ':timed-out-inner)
          (sleep 3)
          ':did-not-time-out))
  :timed-out-inner        ; (after 1 second)
  >


The GBBopen Project


with-lock-heldPortable Threadswithout-lock-heldwith-timeoutGoTo Top