create-and-join-threads.scm 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. (use-modules (ice-9 threads))
  2. (define (sleep-random-time max)
  3. (usleep (random max)))
  4. (define displayln
  5. (λ (sth)
  6. (display (simple-format #f "~a\n" sth))))
  7. (define (make-threads thread-count max-sleep)
  8. (define (iter counter)
  9. (cond [(> counter 0)
  10. (cons (call-with-new-thread
  11. (λ ()
  12. (sleep-random-time max-sleep)
  13. (displayln (simple-format #f "thread ~a done" (- thread-count counter)))))
  14. (iter (- counter 1)))]
  15. [else '()]))
  16. (iter thread-count))
  17. (define (main)
  18. (define max-sleep 500)
  19. (define start-time (current-time))
  20. (displayln (all-threads))
  21. (displayln (current-thread))
  22. (let ([threads (make-threads (current-processor-count) max-sleep)])
  23. (displayln threads)
  24. (map (λ (thread) (join-thread thread #;(+ start-time max-sleep)))
  25. threads)))
  26. ;; See: https://www.gnu.org/software/guile/manual/guile.html#Random
  27. ;; Initializing with randomness from platform.
  28. (set! *random-state* (random-state-from-platform))
  29. (main)