ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Intro.pod
(Generate patch)

Comparing Coro/Coro/Intro.pod (file contents):
Revision 1.10 by root, Sun Aug 22 20:03:35 2010 UTC vs.
Revision 1.11 by root, Sun Feb 6 00:27:43 2011 UTC

180that will wake up some thread. 180that will wake up some thread.
181 181
182 182
183=head2 Semaphores and other locks 183=head2 Semaphores and other locks
184 184
185Using only C<ready>, C<cede> and C<schedule> to synchronise threads 185Using only C<ready>, C<cede> and C<schedule> to synchronise threads is
186is difficult, especially if many threads are ready at the same 186difficult, especially if many threads are ready at the same time. Coro
187time. Coro supports a number of modules to help synchronising threads 187supports a number of primitives to help synchronising threads in easier
188in easier ways. The first such module is L<Coro::Semaphore>, which 188ways. The first such primitives is L<Coro::Semaphore>, which implements
189implements counting semaphores (binary semaphores are also available as 189counting semaphores (binary semaphores are available as L<Coro::Signal>,
190L<Coro::Signal>): 190and there are L<Coro::SemaphoreSet> and L<Coro::RWLock> primitives as
191well):
191 192
192 use Coro; 193 use Coro;
193 use Coro::Semaphore;
194 194
195 my $sem = new Coro::Semaphore 0; # a locked semaphore 195 my $sem = new Coro::Semaphore 0; # a locked semaphore
196 196
197 async { 197 async {
198 print "unlocking semaphore\n"; 198 print "unlocking semaphore\n";
268=head2 Channels 268=head2 Channels
269 269
270Semaphores are fine, but usually you want to communicate by exchanging 270Semaphores are fine, but usually you want to communicate by exchanging
271data as well. This is where L<Coro::Channel> comes in useful: Channels 271data as well. This is where L<Coro::Channel> comes in useful: Channels
272are the Coro equivalent of a unix pipe (and very similar to amiga message 272are the Coro equivalent of a unix pipe (and very similar to amiga message
273ports) - you can put stuff into it on one side, and read data from it on 273ports :) - you can put stuff into it on one side, and read data from it on
274the other. 274the other.
275 275
276Here is a simple example that creates a thread and sends it 276Here is a simple example that creates a thread and sends it
277numbers. The thread calculates the square of the number, and puts it 277numbers. The thread calculates the square of the number, and puts it
278into another channel, which the main thread reads the result from: 278into another channel, which the main thread reads the result from:
279 279
280 use Coro; 280 use Coro;
281 use Coro::Channel;
282 281
283 my $calculate = new Coro::Channel; 282 my $calculate = new Coro::Channel;
284 my $result = new Coro::Channel; 283 my $result = new Coro::Channel;
285 284
286 async { 285 async {
335Be careful, however: when multiple threads put numbers into C<$calculate> 334Be careful, however: when multiple threads put numbers into C<$calculate>
336and read from C<$result>, they won't know which result is theirs. The 335and read from C<$result>, they won't know which result is theirs. The
337solution for this is to either use a semaphore, or send not just the 336solution for this is to either use a semaphore, or send not just the
338number, but also your own private result channel. 337number, but also your own private result channel.
339 338
340L<Coro::Channel> can buffer some amount of items. It is also very 339L<Coro::Channel> can buffer some amount of items.
341instructive to read its source code, as it is very simple and uses two
342counting semaphores internally for synchronisation.
343 340
344 341
345=head2 What is mine, what is ours? 342=head2 What is mine, what is ours?
346 343
347What, exactly, constitutes a thread? Obviously it contains the current 344What, exactly, constitutes a thread? Obviously it contains the current
531In fact, nontrivial programs follow this pattern even with Coro, so a Coro 528In fact, nontrivial programs follow this pattern even with Coro, so a Coro
532program that uses EV usually looks like this: 529program that uses EV usually looks like this:
533 530
534 use EV; 531 use EV;
535 use Coro; 532 use Coro;
536 use Coro::AnyEvent;
537 533
538 # start coroutines or event watchers 534 # start coroutines or event watchers
539 535
540 EV::loop; # and loop 536 EV::loop; # and loop
541 537

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines