mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-19 05:39:17 +02:00
New file
This commit is contained in:
parent
3aad258a76
commit
f432900cb7
2 changed files with 28 additions and 3 deletions
|
@ -1,4 +1,4 @@
|
||||||
;;;; A simple example of using a condition variable to simulate thread-join
|
;;;; An example of using condition variable broadcast to wake other threads.
|
||||||
(import (scheme base)
|
(import (scheme base)
|
||||||
(scheme read)
|
(scheme read)
|
||||||
(scheme write)
|
(scheme write)
|
||||||
|
@ -7,14 +7,16 @@
|
||||||
(define cv (make-condition-variable))
|
(define cv (make-condition-variable))
|
||||||
(define m (make-mutex))
|
(define m (make-mutex))
|
||||||
|
|
||||||
|
;; Thread - Sleep, then wake other threads up via broadcast
|
||||||
(thread-start!
|
(thread-start!
|
||||||
(make-thread
|
(make-thread
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(write "started thread")
|
(write "started thread")
|
||||||
(thread-sleep! 5000)
|
(thread-sleep! 3000)
|
||||||
(condition-variable-broadcast! cv)
|
(condition-variable-broadcast! cv)
|
||||||
(write "thread done"))))
|
(write "thread done"))))
|
||||||
|
|
||||||
|
;; Thread - wait for broadcast
|
||||||
(thread-start!
|
(thread-start!
|
||||||
(make-thread
|
(make-thread
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
@ -22,9 +24,9 @@
|
||||||
(mutex-lock! m)
|
(mutex-lock! m)
|
||||||
(write "register waiting thread cv")
|
(write "register waiting thread cv")
|
||||||
(mutex-unlock! m cv)
|
(mutex-unlock! m cv)
|
||||||
;; TODO: think this is never printed because mutex is locked after waking up..
|
|
||||||
(write "waiting thread done"))))
|
(write "waiting thread done"))))
|
||||||
|
|
||||||
|
;; Main thread - wait for broadcast
|
||||||
(mutex-lock! m)
|
(mutex-lock! m)
|
||||||
(mutex-unlock! m cv) ;; Wait on cv
|
(mutex-unlock! m cv) ;; Wait on cv
|
||||||
(write "main thread done")
|
(write "main thread done")
|
||||||
|
|
23
examples/threading/thread-join.scm
Normal file
23
examples/threading/thread-join.scm
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
;;;; A simple example of using a condition variable to simulate thread-join
|
||||||
|
(import (scheme base)
|
||||||
|
(scheme read)
|
||||||
|
(scheme write)
|
||||||
|
(srfi 18))
|
||||||
|
|
||||||
|
(define cv (make-condition-variable))
|
||||||
|
(define m (make-mutex))
|
||||||
|
|
||||||
|
;; Thread - Do something, then let main thread know when we are done
|
||||||
|
(thread-start!
|
||||||
|
(make-thread
|
||||||
|
(lambda ()
|
||||||
|
(write "started thread")
|
||||||
|
(thread-sleep! 3000)
|
||||||
|
(write "thread done")
|
||||||
|
(condition-variable-broadcast! cv))))
|
||||||
|
|
||||||
|
;; Main thread - wait for thread to broadcast it is done
|
||||||
|
(mutex-lock! m)
|
||||||
|
(mutex-unlock! m cv) ;; Wait on cv
|
||||||
|
(write "main thread done")
|
||||||
|
(thread-sleep! 500)
|
Loading…
Add table
Reference in a new issue