如何以及何时使用 ChezScheme 的定时器中断?

How and when to use ChezScheme's timer interrupt?

Chez的文档提到了set-timertimer-interrupt-handler,但是如何“开球”或合并它们呢?它是用来实现周期性事件还是只是一些延迟操作?

我四处搜索,但只发现它们嵌入到较大的代码块中,无法简单理解。

请提供一些关于如何使用它们的具体代码示例。

https://cisco.github.io/ChezScheme/csug9.5/system.html#./system:h2

(set-timer n) 启动一个内部定时器,在 n 滴答后调用定时器中断处理程序。滴答是 完成的工作量 的粗略度量,而不是时钟时间的度量。如果 scheme 只是坐在 REPL 提示符下,则没有任何工作正在完成,并且不会调用计时器处理程序。

这可能不是您要找的功能。如果你想要一个 clock-based 计时器,你需要自己构建它,可能是 set-timer.

以下代码演示了 set-timer 的用法。但同样,现在不是时钟时间。

$ 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!...........................................
.............................................................................
..................................