Next: , Previous: , Up: iklib unwind-protect   [Index]


6.9.9 Use with coroutines

Using the unwind–protection mechanism with coroutines is fine; coroutines use yield to save the current continuation, give control to the next coroutine and come back later; this mechanism does not cause the ?unwind-handler evaluation.

The following sample code finishes with the given return values and lines printed:

(import (vicare))

(define (print template . args)
  (apply fprintf (current-error-port) template args)
  (yield))

(define a #f)
(define b #f)
(define c #f)

(concurrently
  (lambda ()
    (unwind-protect
        (begin
          (set! a 1.1)
          (print "unwind-protect sub 1.1: ~a\n" a)
          (set! a 1.2)
          (print "unwind-protect sub 1.2: ~a\n" a)
          (set! a 1.3)
          (print "unwind-protect sub 1.3: ~a\n" a))
      (set! a 1.4)))
  (lambda ()
    (unwind-protect
        (begin
          (set! b 2.1)
          (print "unwind-protect sub 2.1: ~a\n" b)
          (set! b 2.2)
          (print "unwind-protect sub 2.2: ~a\n" b)
          (set! b 2.3)
          (print "unwind-protect sub 2.3: ~a\n" b))
      (set! b 2.4)))
  (lambda ()
    (unwind-protect
        (begin
          (set! c 3.1)
          (print "unwind-protect sub 3.1: ~a\n" c)
          (set! c 3.2)
          (print "unwind-protect sub 3.2: ~a\n" c)
          (set! c 3.3)
          (print "unwind-protect sub 3.3: ~a\n" c))
      (set! c 3.4))))

(values a b c)
⇒ 1.4 2.4 3.4
-| unwind-protect sub 1.1: 1.1
-| unwind-protect sub 2.1: 2.1
-| unwind-protect sub 1.2: 1.2
-| unwind-protect sub 3.1: 3.1
-| unwind-protect sub 2.2: 2.2
-| unwind-protect sub 1.3: 1.3
-| unwind-protect sub 3.2: 3.2
-| unwind-protect sub 2.3: 2.3
-| unwind-protect sub 3.3: 3.3