From 2eeae1d7ed2cf81fab9b4ed9ce6d671995763a32 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 13 Jun 2019 13:19:15 -0400 Subject: [PATCH] Refactoring of test code --- examples/Makefile | 9 +++++++++ examples/threading/sum-atomic.scm | 6 +++--- examples/threading/sum-mutex.scm | 4 ++-- examples/threading/sum-nosync.scm | 8 +++----- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 8e665bb1..8a2c571c 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -10,6 +10,9 @@ TARGETS = \ threading/many-writers \ threading/producer-consumer \ threading/thread-join \ + threading/sum-atomic \ + threading/sum-mutex \ + threading/sum-nosync \ game-of-life/life \ hello-library/hello \ @@ -37,6 +40,12 @@ threading/producer-consumer: threading/producer-consumer.scm cyclone $^ threading/thread-join : threading/thread-join.scm cyclone $^ +threading/sum-atomic : threading/sum-atomic.scm + cyclone $^ +threading/sum-mutex : threading/sum-mutex.scm + cyclone $^ +threading/sum-nosync : threading/sum-nosync.scm + cyclone $^ game-of-life/life: cd game-of-life ; make hello-library/hello: diff --git a/examples/threading/sum-atomic.scm b/examples/threading/sum-atomic.scm index 81910c0c..608210c3 100644 --- a/examples/threading/sum-atomic.scm +++ b/examples/threading/sum-atomic.scm @@ -1,4 +1,4 @@ -;;;; A simple example of using a condition variable to simulate thread-join +;;;; Example of having multiple threads sum a variable using an atom. (import (scheme base) (scheme read) (scheme write) @@ -18,7 +18,7 @@ (sum-loop (- n 1)))) (define (sum-entry-pt) - (sum-loop (* 10 100 100 100))) + (sum-loop (* 100 100 100))) ;; Thread - Do something, then let main thread know when we are done (define t1 (make-thread sum-entry-pt)) @@ -50,5 +50,5 @@ (thread-join! t8) (thread-join! t9) (display "main thread done, sum = ") -(display (ref *sum*)) +(display (deref *sum*)) (newline) diff --git a/examples/threading/sum-mutex.scm b/examples/threading/sum-mutex.scm index 9ddeb54d..e27e5216 100644 --- a/examples/threading/sum-mutex.scm +++ b/examples/threading/sum-mutex.scm @@ -1,4 +1,4 @@ -;;;; A simple example of using a condition variable to simulate thread-join +;;;; Example of using a mutex to synchronize summing of a variable by multiple threads. (import (scheme base) (scheme read) (scheme write) @@ -19,7 +19,7 @@ (sum-loop (- n 1)))) (define (sum-entry-pt) - (sum-loop (* 10 100 100 100))) + (sum-loop (* 100 100 100))) ;; Thread - Do something, then let main thread know when we are done (define t1 (make-thread sum-entry-pt)) diff --git a/examples/threading/sum-nosync.scm b/examples/threading/sum-nosync.scm index a22a4b64..7a75f92d 100644 --- a/examples/threading/sum-nosync.scm +++ b/examples/threading/sum-nosync.scm @@ -1,12 +1,10 @@ -;;;; A simple example of using a condition variable to simulate thread-join +;;;; Example of using multiple threads to sum a variable without synchronization. +;;;; Returns inconsistent and wrong results due to a lack of thread coordination. (import (scheme base) (scheme read) (scheme write) (srfi 18)) -;(define cv (make-condition-variable)) -;(define m (make-mutex)) - (define *sum* 0) (define (sum-loop n) @@ -16,7 +14,7 @@ (sum-loop (- n 1)))) (define (sum-entry-pt) - (sum-loop (* 10 100 100 100))) + (sum-loop (* 100 100 100))) ;; Thread - Do something, then let main thread know when we are done (define t1 (make-thread sum-entry-pt))