This commit is contained in:
Justin Ethier 2019-06-11 13:32:12 -04:00
parent d61d78e66b
commit f3d9228d5e
3 changed files with 49 additions and 3 deletions

View file

@ -8,7 +8,7 @@
;(define cv (make-condition-variable))
;(define m (make-mutex))
(define *sum* (make-atom 0))
(define *sum* (make-atom 0.0))
(define (sum-loop n)
;;(set! *sum* (+ *sum* 1))
@ -18,7 +18,7 @@
(sum-loop (- n 1))))
(define (sum-entry-pt)
(sum-loop (* 10 100 100)))
(sum-loop (* 10 10 100 100)))
;; Thread - Do something, then let main thread know when we are done
(define t1 (make-thread sum-entry-pt))

46
libs/test-sum-mutex.scm Normal file
View file

@ -0,0 +1,46 @@
;;;; A simple example of using a condition variable to simulate thread-join
(import (scheme base)
(scheme read)
(scheme write)
(cyclone concurrency)
(srfi 18))
(define m (make-mutex))
(define *sum* 0.0)
(define (sum-loop n)
(mutex-lock! m)
(set! *sum* (+ *sum* 1))
(mutex-unlock! m)
;(swap! *sum* + 1)
(if (zero? n)
(display "thread done\n")
(sum-loop (- n 1))))
(define (sum-entry-pt)
(sum-loop (* 10 10 100 100)))
;; Thread - Do something, then let main thread know when we are done
(define t1 (make-thread sum-entry-pt))
(define t2 (make-thread sum-entry-pt))
(define t3 (make-thread sum-entry-pt))
(define t4 (make-thread sum-entry-pt))
(define t5 (make-thread sum-entry-pt))
(define t6 (make-thread sum-entry-pt))
(thread-start! t1)
(thread-start! t2)
(thread-start! t3)
(thread-start! t4)
(thread-start! t5)
(thread-start! t6)
(thread-join! t1)
(thread-join! t2)
(thread-join! t3)
(thread-join! t4)
(thread-join! t5)
(thread-join! t6)
(display "main thread done, sum = ")
(display *sum*)
(newline)

View file

@ -16,7 +16,7 @@
(sum-loop (- n 1))))
(define (sum-entry-pt)
(sum-loop (* 10 100 100)))
(sum-loop (* 10 100 100 100)))
;; Thread - Do something, then let main thread know when we are done
(define t1 (make-thread sum-entry-pt))