Coroutines are rules that can be suspended with all states ready then resumed on demand multiple times. Each time we resume a coroutine this can produce one result that we capture. So a coroutine can produce multiple results until finishes.
Coroutines can be used as a branch of main thread. This is not yet running in parallel but is an example for how a rule can play as a coroutine.
rule test(n ∈ N) => (result ∈ N): ** can generate n numbers given i ∈ (1..n) do alter result := i; ** suspend execution yield; //wait for the main thread cycle; result = 0; //finalization signal return; ** start secondary process make r ∈ N; // result reference begin test(9) // initialize coroutine do yield r <- test; //capture next result write (r, ","); cycle if r > 0; print; // 0,1,2,3,4,5,6,7,8,9,
Note:&In the example above, the program is still working in serial mode, except that has a branch running on a separated thread. When the coroutine function the main thread is on hold, and when the main thread function the coroutine is waiting.
Producer consumer patern is using coroutines that can work in palallel in asynchronous mode. That means we start coroutines and do not wait to finish but let the main thread to continue. A producer is preparing the work and the consumer is doing the work.
If you do not know how this works, take a look to the diagram below. We have one rule called "producer" one list that act as queue and a routine called "consumer". The queue has a limited capacity. We can use same consumer twice, if we do then the application is also multi-threading..
Read more: Graphics