| Paste number 25629: | Cooperative multi-threader |
| Pasted by: | psykotic |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+JRX |
| Channel: | None |
| Paste contents: |
;;; Cooperative multi-threader
(define *threads* '())
(define *bottom* #f)
(define (push-thread! thread)
(set! *threads* (append *threads* (list thread))))
(define (pop-thread!)
(let ((thread (car *threads*)))
(set! *threads* (cdr *threads*))
thread))
(define (spawn thunk)
(push-thread! (lambda (x) (thunk))))
(define (yield)
(call-with-current-continuation (lambda (k)
(push-thread! k)
(schedule))))
(define (run thunk)
(call-with-current-continuation (lambda (k)
(set! *bottom* k)
(spawn thunk)
(schedule))))
(define (schedule)
(if (null? *threads*)
(*bottom*)
(let ((thunk (pop-thread!)))
(thunk #f)
(schedule))))
Annotations for this paste:
| Annotation number 1: | asfd |
| Pasted by: | asdf |
| When: | 6 years, 8 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+JRX/1 |
| Paste contents: |
asfd