Created
November 3, 2019 23:57
-
-
Save chelseatroy/cd87603d92268f5f7931abf97e228233 to your computer and use it in GitHub Desktop.
Queue Concurrency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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