Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

coro::channel(3pm) [debian man page]

Channel(3pm)						User Contributed Perl Documentation					      Channel(3pm)

NAME
Coro::Channel - message queues SYNOPSIS
use Coro; $q1 = new Coro::Channel <maxsize>; $q1->put ("xxx"); print $q1->get; die unless $q1->size; DESCRIPTION
A Coro::Channel is the equivalent of a unix pipe (and similar to amiga message ports): you can put things into it on one end and read things out of it from the other end. If the capacity of the Channel is maxed out writers will block. Both ends of a Channel can be read/written from by as many coroutines as you want concurrently. You don't have to load "Coro::Channel" manually, it will be loaded automatically when you "use Coro" and call the "new" constructor. $q = new Coro:Channel $maxsize Create a new channel with the given maximum size (practically 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, and "put" will wait until there is a corresponding "get" call). 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->shutdown Shuts down the Channel by pushing a virtual end marker onto it: This changes the behaviour of the Channel when it becomes or is empty to return "undef", almost as if infinitely many "undef" elements have been put into the queue. Specifically, this function wakes up any pending "get" calls and lets them return "undef", the same on future "get" calls. "size" will return the real number of stored elements, though. Another way to describe the behaviour is that "get" calls will not block when the queue becomes empty but immediately return "undef". This means that calls to "put" will work normally and the data will be returned on subsequent "get" calls. This method is useful to signal the end of data to any consumers, quite similar to an end of stream on e.g. a tcp socket: You have one or more producers that "put" data into the Channel and one or more consumers who "get" them. When all producers have finished producing data, a call to "shutdown" signals this fact to any consumers. $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 instead works just fine. Note that the number of elements that wait can be larger than $maxsize, as it includes any coroutines waiting to put data into the channel (but not any shutdown condition). This means that the number returned is precisely the number of calls to "get" that will succeed instantly and return some data. Calling "shutdown" has no effect on this number. AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ perl v5.14.2 2012-04-13 Channel(3pm)

Check Out this Related Man Page

SemaphoreSet(3pm)					User Contributed Perl Documentation					 SemaphoreSet(3pm)

NAME
Coro::SemaphoreSet - efficient set of counting semaphores SYNOPSIS
use Coro; $sig = new Coro::SemaphoreSet [initial value]; $sig->down ("semaphoreid"); # wait for signal # ... some other "thread" $sig->up ("semaphoreid"); DESCRIPTION
This module implements sets of counting semaphores (see Coro::Semaphore). It is nothing more than a hash with normal semaphores as members, but is more efficiently managed. This is useful if you want to allow parallel tasks to run in parallel but not on the same problem. Just use a SemaphoreSet and lock on the problem identifier. You don't have to load "Coro::SemaphoreSet" manually, it will be loaded automatically when you "use Coro" and call the "new" constructor. new [inital count] Creates a new semaphore set with the given initial lock count for each individual semaphore. See Coro::Semaphore. $semset->down ($id) Decrement the counter, therefore "locking" the named semaphore. This method waits until the semaphore is available if the counter is zero. $semset->up ($id) Unlock the semaphore again. If the semaphore reaches the default count for this set and has no waiters, the space allocated for it will be freed. $semset->try ($id) Try to "down" the semaphore. Returns true when this was possible, otherwise return false and leave the semaphore unchanged. $semset->count ($id) Return the current semaphore count for the specified semaphore. $semset->waiters ($id) Returns the number (in scalar context) or list (in list context) of waiters waiting on the specified semaphore. $semset->wait ($id) Same as Coro::Semaphore::wait on the specified semaphore. $guard = $semset->guard ($id) This method calls "down" and then creates a guard object. When the guard object is destroyed it automatically calls "up". $semaphore = $semset->sem ($id) This SemaphoreSet version is based on Coro::Semaphore's. This function creates (if necessary) the underlying Coro::Semaphore object and returns it. You may legally call any Coro::Semaphore method on it, but note that calling "$semset->up" can invalidate the returned semaphore. AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ perl v5.14.2 2012-04-13 SemaphoreSet(3pm)
Man Page