Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,44 @@
#lang racket
(define t 5) ; total number of threads
(define count 0) ; number of threads arrived at rendezvous
(define mutex (make-semaphore 1)) ; exclusive access to count
(define turnstile (make-semaphore 0))
(define turnstile2 (make-semaphore 1))
(define ch (make-channel))
(define (make-producer name start)
(λ ()
(let loop ([n start])
(sleep (* 0.01 (random 10))) ; "compute" something
;; rendezvous
(semaphore-wait mutex)
(set! count (+ count 1)) ; we have arrived
(when (= count t) ; are we the last to arrive?
(semaphore-wait turnstile2)
(semaphore-post turnstile))
(semaphore-post mutex)
; avoid deadlock problem:
(semaphore-wait turnstile)
(semaphore-post turnstile)
; critical point
(channel-put ch n) ; send result to controller
; leave properly
(semaphore-wait mutex)
(set! count (- count 1))
(when (= count 0) ; are we the last to leave?
(semaphore-wait turnstile)
(semaphore-post turnstile2))
(semaphore-post mutex)
(semaphore-wait turnstile2)
(semaphore-post turnstile2)
(loop (+ n t)))))
; start t workers:
(map (λ(start) (thread (make-producer start start)))
(range 0 t))
(let loop ()
(displayln (for/list ([_ t]) (channel-get ch)))
(loop))

View file

@ -0,0 +1,21 @@
(1 4 2 0 3)
(6 9 7 8 5)
(11 10 14 12 13)
(16 15 18 19 17)
(24 21 20 23 22)
(29 25 28 27 26)
(30 33 34 32 31)
(37 38 39 35 36)
(44 43 41 40 42)
(46 45 48 49 47)
(50 53 51 54 52)
(56 57 58 55 59)
(60 63 62 61 64)
(66 69 65 68 67)
(73 70 74 71 72)
(78 77 76 79 75)
(82 80 81 84 83)
(87 89 88 86 85)
(92 93 90 91 94)
(97 98 99 95 96)
...