aboutsummaryrefslogtreecommitdiff
path: root/concurrentreadandprint.rkt
diff options
context:
space:
mode:
authorIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-05 15:30:10 -0400
committerIbrahim Mkusa <ibrahimmkusa@gmail.com>2017-04-05 15:30:10 -0400
commita93d17e0e65458e8dbadf5d510229b0a9c9e1470 (patch)
tree67c86d33d8c09eac4303b858a27c16e8f2ba9cce /concurrentreadandprint.rkt
parentf86c333ce56a5e0b714c737c7e083767fe283fb9 (diff)
Two threads of execution example
Diffstat (limited to 'concurrentreadandprint.rkt')
-rw-r--r--concurrentreadandprint.rkt51
1 files changed, 51 insertions, 0 deletions
diff --git a/concurrentreadandprint.rkt b/concurrentreadandprint.rkt
new file mode 100644
index 0000000..2272d97
--- /dev/null
+++ b/concurrentreadandprint.rkt
@@ -0,0 +1,51 @@
+#lang racket
+;; author: Ibrahim Mkusa
+;; about: print and read concurrently
+
+;; reads values continously from stdin and redisplays them
+(define (read-loop)
+ (display (read-line))
+ (display "\n")
+ (read-loop)
+ )
+
+(define input-prompt "input: ")
+(define output-prompt "output: ")
+
+;; prompt for username and bind to a variable username
+(display "What's your name?\n")
+(define username (read-line))
+(define usernamei (string-append username ": ")) ;; make username appear nicer in a prompt
+(define fair (make-semaphore 1))
+
+;; intelligent read, quits when user types in "quit"
+(define (read-loop-i)
+ (display usernamei)
+
+ (semaphore-wait fair)
+ (define input (read-line))
+ ;; do something over here with input maybe send it out
+ (cond ((string=? input "quit") (exit)))
+ (display (string-append output-prompt input "\n"))
+ (semaphore-post fair)
+ (read-loop-i)
+ )
+
+
+;; print hello world continously
+(define (hello-world)
+ (semaphore-wait fair)
+ (display "Hello, World!\n")
+ (semaphore-post fair)
+ (hello-world))
+
+(define t (thread (lambda ()
+ (read-loop-i))))
+(define a (thread (lambda ()
+ (hello-world))))
+;; below doesn't execute
+; (sleep 10)
+; (kill-thread t)
+; (define a (thread (display "hello world!\n")))
+; (display "John: hello soso\n")
+; (display "Emmanuel: cumbaya!!!!\n") \ No newline at end of file