Skip to content

Instantly share code, notes, and snippets.

@b4284
Created January 4, 2019 20:01
Show Gist options
  • Save b4284/86f53ec29cb5aefe759f2e338fc89872 to your computer and use it in GitHub Desktop.
Save b4284/86f53ec29cb5aefe759f2e338fc89872 to your computer and use it in GitHub Desktop.
(use-modules (ice-9 threads))
(define mtx (make-mutex))
(define cv (make-condition-variable))
(define v 0)
(lock-mutex mtx) ;; Lock first to declare master thread
(define t
(call-with-new-thread
(lambda ()
(let A ()
(lock-mutex mtx)
(set! v (+ v 1))
(format #t "A: v=~a~%" v)
(signal-condition-variable cv)
(wait-condition-variable cv mtx)
(unlock-mutex mtx)
(A)))))
(let B ()
(set! v (+ v 1))
(format #t "B: v=~a~%" v)
(signal-condition-variable cv)
(wait-condition-variable cv mtx)
(B))
(join-thread t)
;; (let B ()
;; (set! v (+ v 1))
;; (format #t "B: v=~a~%" v)
;; (unlock-mutex mtx)
;; (signal-condition-variable cv)
;; (B))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment