RosettaCodeData/Task/Synchronous-concurrency/TXR/synchronous-concurrency.txr
2016-12-05 22:15:40 +01:00

33 lines
735 B
Text

(defstruct thread nil
suspended
cont
(:method resume (self)
[self.cont])
(:method give (self item)
[self.cont item])
(:method get (self)
(yield-from run nil))
(:method start (self)
(set self.cont (obtain self.(run)))
(unless self.suspended
self.(resume)))
(:postinit (self)
self.(start)))
(defstruct consumer thread
(count 0)
(:method run (self)
(whilet ((item self.(get)))
(prinl item)
(inc self.count))))
(defstruct producer thread
consumer
(:method run (self)
(whilet ((line (get-line)))
self.consumer.(give line))))
(let* ((con (new consumer))
(pro (new producer suspended t consumer con)))
pro.(resume)
(put-line `count = @{con.count}`))