Adapting from the docs of wills and executors, this works as I expected:
#lang racket
(define an-executor (make-will-executor))
(void (thread (λ () (sync an-executor) (will-execute an-executor))))
(define (executor-proc v) (printf "a-box is now garbage\n"))
(define a-box-to-track (box #f))
(will-register an-executor a-box-to-track executor-proc)
(collect-garbage)
(set! a-box-to-track #f)
(collect-garbage)
but apparently I can't rely on program termination to execute the will, that is, if (collect-garbage)
isn't called explicitly, the will is not executed:
#lang racket
(define an-executor (make-will-executor))
(void (thread (λ () (sync an-executor) (will-execute an-executor))))
(define (executor-proc v) (printf "a-box is now garbage\n"))
(define a-box-to-track (box #f))
(will-register an-executor a-box-to-track executor-proc)
(I would have expected all programs to finish with some form of (collect-garbage)
.)
What's the proper way to ensure that the will is executed?
Specifically, the objective is to delete a temporary file upon program exit (or upon unreachability of a value).