ThreadIterator

 view release on metacpan or  search on metacpan

Iterator.pm  view on Meta::CPAN

package Thread::Iterator;
use Thread qw(async);
use Thread::Semaphore;
use Thread::Queue;
use strict;
use vars qw($VERSION @ISA @EXPORT_OK);
use Exporter;
$VERSION = 0.1;
@ISA = 'Exporter';
@EXPORT_OK = 'coroutine';

sub _iter {
    my ($code, $obj) = @_;
    &$code($obj);
    $obj->[2] = 0;
    $obj->[0]->enqueue(undef);
}

sub provide {
    my ($obj, $val) = @_;

Iterator.pm  view on Meta::CPAN


sub new {
    my ($class, $code) = @_;
    my $sem = Thread::Semaphore->new(0);
    my $q = Thread::Queue->new;
    my $obj = bless [$q, $sem, 1], $class;
    Thread->new(\&_iter, $code, $obj);
    return $obj;
}

sub coroutine (&) {
    return __PACKAGE__->new($_[0]);
}

1;

README  view on Meta::CPAN

    the GNU General Public License or the Artistic License for more details.

    You should have received a copy of the Artistic License with this kit,
    in the file named "Artistic".  If not, you can get one from the Perl
    distribution. You should also have received a copy of the GNU General
    Public License, in the file named "Copying". If not, you can get one
    from the Perl distribution or else write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

This module is a proof-of-concept implementation of
iterators/coroutines in Perl using threads.

CHANGES

0.1  13 Aug 1998
Initial release.

INSTALLATION

You need Perl 5.005 or later with thread support configured
(Configure -Dusethreads). This module consists of a single Perl module

iterator.t  view on Meta::CPAN

use Thread::Iterator 'coroutine';

$c = coroutine {
    my $obj = shift;
    for (my $i = 0; $i < 10; $i++) {
	$obj->provide($i);
   }
};

for ($j = 0; $j < 12; $j++) {
    printf "iterator is %s, ", $c->alive ? "alive" : "dead";
    my $val = $c->iterate;
    printf "value is %s\n", defined($val) ? $val : "undefined";



( run in 1.368 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )