The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Coro::Channel - message queues

SYNOPSIS

 use Coro::Channel;

 $q1 = new Coro::Channel <maxsize>;

 $q1->put ("xxx");
 print $q1->get;

 die unless $q1->size;

DESCRIPTION

A Coro::Channel is the equivalent of a pipe: you can put things into it on one end end read things out of it from the other hand. If the capacity of the Channel is maxed out writers will block. Both ends of a Channel can be read/written from as many coroutines as you want.

$q = new Coro:Channel $maxsize

Create a new channel with the given maximum size (unlimited if maxsize is omitted). Giving a size of one gives you a traditional channel, i.e. a queue that can store only a single element (which means there will be no buffering). To buffer one element you have to specify 2, and so on.

$q->put ($scalar)

Put the given scalar into the queue.

$q->get

Return the next element from the queue, waiting if necessary.

$q->timed_get ($timeout)

Return the next element from the queue, waiting up to $timeout seconds if necessary. If no element arrives within the given time an empty list will be returned.

$q->size

Return the number of elements waiting to be consumed. Please note that:

  if ($q->size) {
     my $data = $q->get;
  }

is NOT a race condition but works fine.

AUTHOR

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/