You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to create a classic producer/consumer using fibers. The producer fills up an array until it's full (3 items), and then passes control to the consumer which uses up elements one by one until the array is empty, and then passes control back to the producer. Control just bounces back and forth unless/until I break in and tell it to stop fooling around. I've got this so far:
#!/usr/bin/env janet
(defarr @[])
(defnproducer``Fills up `arr` with three "@" signs.``
[]
(while (< (lengtharr) 4)
(pparr)
(array/pusharr"@"))
(print"---")
(yield))
(defnconsumer``Uses up the elements in `arr` until empty.``
[]
(while (not (empty?arr))
(array/poparr)
(pparr))
(print"---")
(resumeproducer))
(defnmain
[&]
(deffbr (fiber/newproducer))
(resumefbr))
but I know it's not right, and anyway, it just ends after producer finishes:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I want to create a classic producer/consumer using fibers. The producer fills up an array until it's full (3 items), and then passes control to the consumer which uses up elements one by one until the array is empty, and then passes control back to the producer. Control just bounces back and forth unless/until I break in and tell it to stop fooling around. I've got this so far:
but I know it's not right, and anyway, it just ends after
producerfinishes:How can I fix this and get it to work? I think I want producer to yield to consumer, and consumer to likewise yield to producer...
Beta Was this translation helpful? Give feedback.
All reactions