Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Queue Concurrency
(define ready-to-call (make-queue))
(define (call-soon proc)
((ready-to-call 'insert!) proc))
(define (run)
(cond ((ready-to-call 'empty?) 'done)
(else
(let ((proc (ready-to-call 'front)))
(proc) ; execute the 0-argument lambda
(ready-to-call 'delete!)))))
(define (countdown n)
(cond ((= n 0) 'countdown-done)
(else
(display "Down ")
(displayln n)
(call-soon (lambda () (countdown (- n 1)))))))
(define (up stop)
(define (iter x)
(cond ((> x stop) 'up-done)
(else
(display "Up ")
(displayln x)
(call-soon (lambda () (iter (+ x 1)))))))
(iter 0))
(call-soon (lambda () (countdown 3)))
(call-soon (lambda () (up 3)))
(run)
; Down 3
; Up 1
; Down 2
; Up 2
; Down 1
; Up 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.