Created
July 23, 2021 13:31
-
-
Save ryloric/038c4b13fbcb9d8a9c767959277a08ae to your computer and use it in GitHub Desktop.
This file contains 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
(import srfi-18) | |
(import (chicken format)) | |
(import (chicken random)) | |
;; Compute fib(n) | |
(define (fib n) | |
(if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) | |
;; Compute and print all fib until n, yield after each fib computation | |
(define (fib-seq n) | |
(do ((i 0 (+ i 1))) | |
((= i n) '()) | |
(printf "~A | Fib(~A) = ~A~N" (thread-name (current-thread)) i (fib i)) | |
(thread-yield!))) | |
;; Run a function f m times per thread in n threads | |
(define (run n m) | |
(let ((threads-1 (do ((i 0 (+ i 1)) (l '())) | |
((= i n) l) | |
(set! l (cons (make-thread (lambda () (busy-loop)) (sprintf "T~A" i)) l))))) | |
(for-each (lambda (t) (thread-join! (thread-start! t))) threads-1))) | |
(run 10 31) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment