Skip to content

Instantly share code, notes, and snippets.

@iitalics
Created April 14, 2020 18:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iitalics/8bf2aa8e62b03045ad023287f9a7f6b2 to your computer and use it in GitHub Desktop.
Save iitalics/8bf2aa8e62b03045ad023287f9a7f6b2 to your computer and use it in GitHub Desktop.
#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