Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created November 3, 2019 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/cd87603d92268f5f7931abf97e228233 to your computer and use it in GitHub Desktop.
Save chelseatroy/cd87603d92268f5f7931abf97e228233 to your computer and use it in GitHub Desktop.
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