Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Coroutines in Perl

by theorbtwo (Prior)
on May 07, 2004 at 06:19 UTC ( [id://351371]=note: print w/replies, xml ) Need Help??


in reply to Coroutines in Perl

Interestingly, perl6 allows for coroutines. Parrot will be largely based on a closely related concept, called continuations.

(As far as I can tell, a continuation is a coroutine with it's state wrapped in an object.)

They're extremely useful in certian cases.

Think of a CGI script. If you have continuations, you can pretend that the entire conversation with the user takes place within one coherent program, and that you're waiting for the user to respond, instead of being re-invoked on every return.

Think of an iterator, that was written in terms of continuing it's running, instead of calling into the same sub over and over.

It's possible to write both things without using contations, but contuations can produce cleaner code... if used carefuly, and in the right places. (Much like threads, which are also related. Like threads, they can be tricky to get working properly, and they can produce ugly and innefficent code if used poorly.)

Replies are listed 'Best First'.
Re: Re: Coroutines in Perl
by mrd (Beadle) on May 07, 2004 at 17:46 UTC

    The main diffrenece between coroutines and threads is that threads are meant to run in "paralel" (and they *really* do on multiprocessor machines), while coroutines are not. Therefore, when using coroutines, there is no need to worry about synchronisation, race conditions, starvation, you know ... stuff that plagues "multithreaded programming".

    For an excelent discussion of coroutines and how they work in Lua, see this paper:

    http://www.inf.puc-rio.br/~roberto/docs/corosblp.pdf

      Threads are also pre-emptive, even on a single CPU. Coroutines are cooperatively yielding.
      threads are meant to run in "paralel"..., while coroutines are not. Therefore, when using coroutines, there is no need to worry about synchronisation, race conditions, starvation, you know ...
      They do effectively run in parallel, and you can get synchronisation problems. These are a symptom of any non-linear algorithm, whether it uses threading or not.
        By "parallel" I meant "concurrently".

        And there can be no two coroutines running at the same time. There is always only one coroutine running, while all others are waiting for it to "yield" or "return".

        What synchronisation problems with coroutines are you referring to?

        Implementing a classic "producer-consumer" system using coroutines is straight-forward and natural. No extra mechanisms are necesary to ensure that the "consumer" is reading "valid data" or that the "producer" isn't overwitting unprecessed (by the consumer) data.

        Now, one can build coroutines on top of threads, as Python does (*), but that is just an implementation detail that a programmer doesn't need to care about.

        (*) I'm referring to the "Coroutine.py" library distributed with Python, which for some reason isn't mentioned as part of the "Python Standard Library".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://351371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found