1Channel(3)            User Contributed Perl Documentation           Channel(3)
2
3
4

NAME

6       Coro::Channel - message queues
7

SYNOPSIS

9        use Coro;
10
11        $q1 = new Coro::Channel <maxsize>;
12
13        $q1->put ("xxx");
14        print $q1->get;
15
16        die unless $q1->size;
17

DESCRIPTION

19       A Coro::Channel is the equivalent of a unix pipe (and similar to amiga
20       message ports): you can put things into it on one end and read things
21       out of it from the other end. If the capacity of the Channel is maxed
22       out writers will block. Both ends of a Channel can be read/written from
23       by as many coroutines as you want concurrently.
24
25       You don't have to load "Coro::Channel" manually, it will be loaded
26       automatically when you "use Coro" and call the "new" constructor.
27
28       $q = new Coro:Channel $maxsize
29           Create a new channel with the given maximum size (practically
30           unlimited if "maxsize" is omitted or zero). Giving a size of one
31           gives you a traditional channel, i.e. a queue that can store only a
32           single element (which means there will be no buffering, and "put"
33           will wait until there is a corresponding "get" call). To buffer one
34           element you have to specify 2, and so on.
35
36       $q->put ($scalar)
37           Put the given scalar into the queue.
38
39       $q->get
40           Return the next element from the queue, waiting if necessary.
41
42       $q->shutdown
43           Shuts down the Channel by pushing a virtual end marker onto it:
44           This changes the behaviour of the Channel when it becomes or is
45           empty to return "undef", almost as if infinitely many "undef"
46           elements had been put into the queue.
47
48           Specifically, this function wakes up any pending "get" calls and
49           lets them return "undef", the same on future "get" calls. "size"
50           will return the real number of stored elements, though.
51
52           Another way to describe the behaviour is that "get" calls will not
53           block when the queue becomes empty but immediately return "undef".
54           This means that calls to "put" will work normally and the data will
55           be returned on subsequent "get" calls.
56
57           This method is useful to signal the end of data to any consumers,
58           quite similar to an end of stream on e.g. a tcp socket: You have
59           one or more producers that "put" data into the Channel and one or
60           more consumers who "get" them. When all producers have finished
61           producing data, a call to "shutdown" signals this fact to any
62           consumers.
63
64           A common implementation uses one or more threads that "get" from a
65           channel until it returns "undef". To clean everything up, first
66           "shutdown" the channel, then "join" the threads.
67
68       $q->size
69           Return the number of elements waiting to be consumed. Please note
70           that:
71
72             if ($q->size) {
73                my $data = $q->get;
74                ...
75             }
76
77           is not a race condition but instead works just fine. Note that the
78           number of elements that wait can be larger than $maxsize, as it
79           includes any coroutines waiting to put data into the channel (but
80           not any shutdown condition).
81
82           This means that the number returned is precisely the number of
83           calls to "get" that will succeed instantly and return some data.
84           Calling "shutdown" has no effect on this number.
85

AUTHOR/SUPPORT/CONTACT

87          Marc A. Lehmann <schmorp@schmorp.de>
88          http://software.schmorp.de/pkg/Coro.html
89
90
91
92perl v5.32.0                      2020-08-03                        Channel(3)
Impressum