Last active
July 14, 2018 15:07
-
-
Save daviderestivo/6482b07b26a116328ce5df7d7c5921a4 to your computer and use it in GitHub Desktop.
Emacs 26 - Threads and conditional variables example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Unset var1 | |
(setq var1 nil) | |
;; Create a mutex and an associated conditional variable | |
(setq mut1 (make-mutex "mut1")) | |
(setq cond-var1 (make-condition-variable mut1 "cond-var1")) | |
;; Read and set functions used by r1 and s1 threads | |
(defun read-global-var1 () | |
(with-mutex mut1 | |
(while (not var1) | |
(message "var1 still not been set ... looping") | |
;; condition-wait is a blocking function. The thread will block here. | |
(condition-wait cond-var1)) | |
(concat (message "var1 has been set ... exiting") var1))) | |
(defun set-global-var1 () | |
(with-mutex mut1 | |
(progn | |
(sleep-for 5) | |
(setq var1 "1") | |
(condition-notify cond-var1)))) | |
;; Create and start r1 thread | |
(make-thread 'read-global-var1 "r1") | |
(all-threads) | |
;; Create and start s1 | |
(make-thread 'set-global-var1 "s1") | |
(all-threads) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment