Skip to content

Instantly share code, notes, and snippets.

@ryloric
Created July 23, 2021 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryloric/038c4b13fbcb9d8a9c767959277a08ae to your computer and use it in GitHub Desktop.
Save ryloric/038c4b13fbcb9d8a9c767959277a08ae to your computer and use it in GitHub Desktop.
(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