Created
April 1, 2022 20:42
-
-
Save samdphillips/e8fdc90b284a5519976127cd902dc312 to your computer and use it in GitHub Desktop.
threaded box thingy
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
#lang racket/base | |
(require racket/match) | |
(struct holder (thd req-ch)) | |
(define (make-holder initial-value) | |
(define req-ch (make-channel)) | |
(define thd | |
(thread | |
(lambda () | |
(define (run current-value) | |
(match-define (list update return) (channel-get req-ch)) | |
(define new-value (update current-value)) | |
(return current-value new-value) | |
(run new-value)) | |
(run initial-value)))) | |
(holder thd req-ch)) | |
(define (holder-send h update return) | |
(channel-put (holder-req-ch h) (list update return))) | |
(define (holder-send/sync h update) | |
(define rsp-ch (make-channel)) | |
(define (reply old new) | |
(channel-put rsp-ch (list old new))) | |
(holder-send h update reply) | |
(channel-get rsp-ch)) | |
(define (holder-send/async h update) | |
(holder-send h update void)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment