Created
October 9, 2022 21:05
-
-
Save samdphillips/104c52d351ed59d0d6c3d32dd96b842c to your computer and use it in GitHub Desktop.
places work channel sharing
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/place) | |
(define-logger guess) | |
(define num-workers (processor-count)) | |
(define secret-number (random 10)) | |
(define-values (manager-work-ch worker-work-ch) (place-channel)) | |
(define (process-work waiting) | |
(define (check-result v) | |
(unless (= secret-number v) | |
(process-work (add1 waiting)))) | |
(cond | |
[(zero? waiting) (sync (handle-evt manager-work-ch check-result))] | |
[else | |
(log-guess-info "sending work") | |
(place-channel-put manager-work-ch (random 4)) | |
(process-work (sub1 waiting))])) | |
(define workers | |
(for/list ([i num-workers]) | |
(define w (dynamic-place "worker.rkt" 'worker-main)) | |
(place-channel-put w worker-work-ch) | |
w)) | |
(process-work num-workers) | |
(log-guess-info "number guessed") | |
(for ([w (in-list workers)]) (place-break w)) |
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/place) | |
(provide worker-main) | |
(define-logger guess-worker) | |
(define (worker-main pch) | |
(define (run) | |
(define wait (place-channel-get worker-work-ch)) | |
(log-guess-worker-info "waiting ~a" wait) | |
(sleep wait) | |
(place-channel-put worker-work-ch (random 10)) | |
(run)) | |
(define worker-work-ch (place-channel-get pch)) | |
(run)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment