The document of Chez mentioned set-timer
and timer-interrupt-handler
, but how to "kick off" or combine them? Is it used to implement things like periodic events or just some delayed operation?
I searched around but only find them embedded into larger code chunks which cannot be trivially understood.
Please provide some concrete code examples on how to use them.
https://cisco.github.io/ChezScheme/csug9.5/system.html#./system:h2
(set-timer n)
starts an internal timer that calls the timer interrupt handler after n
ticks. A tick is a rough measure of the amount of work being done, not a measure of clock time. If scheme is just sitting at the REPL prompt, no work is being done and the timer handler will not be called.
This is probably not the functionality you're looking for. If you want a clock-based timer, you will need to build it yourself, possibly in terms of set-timer
.
The following code demonstrates use of set-timer
. But again, it's not clock time.
$ scheme
Chez Scheme Version 9.5.8
Copyright 1984-2022 Cisco Systems, Inc.
> (define (start-periodic-timer n f)
(timer-interrupt-handler (lambda ()
(f)
(set-timer n)))
(set-timer n))
> (start-periodic-timer 3000 (lambda () (printf "timer!")))
0
> (do ([i 0 (+ i 1)]) ((= i 2000) (newline)) (printf "."))
timer!timer!timer!timer!.....................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
....................................timer!...................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
.............................................................................
............................timer!...........................................
.............................................................................
..................................