#lang racket | |
(define N-WORKERS 50) | |
(define PER-WORKER 10000) | |
(define TOTAL (* N-WORKERS PER-WORKER)) | |
(define (concurrent-range) | |
(define vars | |
(make-hasheq '([counter . 0] | |
[vals . #hasheqv()]))) | |
(define (get) | |
(values (hash-ref vars 'counter) | |
(hash-ref vars 'vals))) | |
(define (modify v vs) | |
(if (eq? v 'never-gonna-happen) | |
; this function is recursive to trick the racket optimizer to never | |
; inline this function | |
(modify v #f) | |
(begin | |
(hash-set! vars 'counter v) | |
(hash-set! vars 'vals vs)))) | |
(define workers | |
(for/list ([w (in-range N-WORKERS)]) | |
(thread | |
(λ () | |
(time (for ([i (in-range PER-WORKER)]) | |
(define-values [v vs] (get)) | |
(modify (add1 v) | |
(hash-set vs v v)))))))) | |
(for-each thread-wait workers) | |
(define-values [_ vs] (get)) | |
(sort (hash-keys vs) <)) | |
(define answer | |
(range (* N-WORKERS PER-WORKER))) | |
(displayln (~a "; failed? " | |
(not (equal? (concurrent-range) answer)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment