How does one do the equivilant of the C select() function call in Common Lisp?
This is something I could never figure out all those years I used a Symbolics. No
big deal, just curious.
Thanks,
Mike McDonald Advanced Technology Dept.
Harris Corp.
Email: ···@trantor.harris-atd.com M.S. 16-1912
Voice: (407) 727-5060 P.O. Box 37
Fax: (407) 729-3363 Melbourne, Florida 32902
In article <··········@jabba.ess.harris.com> ···@mercury.Harris-ATD.com (Mike McDonald) writes:
> How does one do the equivilant of the C select() function call in Common Lisp?
>This is something I could never figure out all those years I used a Symbolics.
Common Lisp doesn't have any support for non-blocking output, so I'll give
a simple solution that works only for input streams. Unfortunately, it
uses polling.
(defun input-select (streams timeout)
"Wait until any of STREAMS has input available or TIMEOUT seconds elapse.
Returns the list of ready streams or () to indicate a timeout."
(loop with end-time = (+ (get-universal-time) timeout)
when (>= (get-universal-time) end-time)
return '()
when (loop for stream in streams
when (listen stream)
collect stream)
return it
;; don't chew up CPU cycles
do (sleep 1)))
This isn't how I'd do it if I were writing non-portable code for a
Symbolics system; in that case I'd use PROCESS-WAIT or something like that.
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar
From: Jeff Dalton
Subject: Re: select() in CL??
Date:
Message-ID: <8685@skye.ed.ac.uk>
In article <··········@jabba.ess.harris.com> ···@mercury.Harris-ATD.com (Mike McDonald) writes:
>
>How does one do the equivilant of the C select() function call in
>Common Lisp?
There isn't a portable way to do it. In many Lisps, you could
write some C code that calls select. Otherwise you have to do
something like this:
pseudo_select [streams, time_limit] =
loop
if the current time is >= the time_limit
then return
else if listen returns true for any stream
then return
else sleep for 1 second
pool
(but in Lisp, of course).
-- jd