Synchronous concurrency: Difference between revisions

Content added Content deleted
m (Added comment for two synchronous tasks)
(Added coroutine-version)
Line 788: Line 788:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
PicoLisp has no threads, but synchronous background tasks and asynchronous
PicoLisp has no threads, but synchronous background tasks and asynchronous
signal handlers.
signal handlers, or coroutines.


===Using background tasks and signals===
The following two tasks communicate via UDP, so in fact they don't need to run
The following two tasks communicate via UDP, so in fact they don't need to run
within the same process and not even the same machine. "input.txt" would rather
within the same process and not even the same machine. "input.txt" would rather
Line 816: Line 817:
If the two cases of 'sigio' in the printing task are replaced with 'task',
If the two cases of 'sigio' in the printing task are replaced with 'task',
that task would also be synchronous. The resulting behavior is the same.
that task would also be synchronous. The resulting behavior is the same.

===Using coroutines===
Coroutines are available only in the 64-bit version.
<lang PicoLisp>(co 'unit1
(yield) # Allow 'unit2' to start
(in "input.txt" # Read the file
(while (line T) # Send each line
(yield @ 'unit2) ) ) # to 'unit2'
(prinl
(yield NIL 'unit2) # Send 'NIL' for "Done", receive count
" lines" ) )

(co 'unit2
(let Cnt 0 # Init counter
(while (yield NIL 'unit1) # Receive line
(println @) # Print it
(inc 'Cnt) ) # Increment count
(yield Cnt 'unit1) ) ) # Send count to 'unit1'</lang>


=={{header|Python}}==
=={{header|Python}}==