Hi František,

> I've used the following method in other programming languages (Lua, Janet)
> and I'm wondering whether I can do something similar in PicoLisp:
> 
> I've got a coroutine that generates an infinite sequence of data.
> ...
> The upshot of this method is that I don't have to manually kill any of the
> coroutines, they are all automatically garbage collected when I no longer
> use them.

In PicoLisp, execution and garbage collection of coroutines are related
in this way:

While a coroutine is running - *independent* from whether it is
referenced from anywhere else - it will not be garbage collected.

Only if it finished execution (either by dropping off the end of its
code, by doing a 'throw' outside itself, or by being explicitly stopped
by another (co)routine), it will be garbage collected. Collected are
then all data which are referenced from the now freed stack segment.


> If I understand it correctly, creating a coroutine in PicoLisp creates
> a global symbol that keeps the reference to this coroutine

This not correct. The coroutine does not create a symbol. It is the Lisp
*reader* which finds or creates a symbol 'myCoroutine' when reading an
expression like

   (co 'myCoroutine (...))

But this symbol is just a tag to access the coroutine. It is not
relevant for garbage collecting the coroutine itself.

This symbol does not need to be global. You can use a transient symbol

   (co "myCoroutine" (...))

and thus have a file-local scope, or use a namespace.

The transient symbol may go out of scope, but the coroutine continues to
exist until it terminates as described above.


> explicitly remove the coroutine when I no longer need it - it can never get
> automatically garbage collected because it's linked to a global symbol.

So you indeed need to call

   (co 'myCoroutine)

if you are not sure if it did not already terminate by itself. But this
has nothing to do with the tag symbol.

Let's clear up this in IRC :)

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to