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.9 by root, Mon Jul 27 02:45:30 2009 UTC vs.
Revision 1.10 by root, Sun Aug 22 20:03:35 2010 UTC

105 main 2 105 main 2
106 async 2 106 async 2
107 107
108This nicely illustrates the non-local jump ability: the main program 108This nicely illustrates the non-local jump ability: the main program
109prints the first line, and then yields the CPU to whatever other 109prints the first line, and then yields the CPU to whatever other
110threads there are. And there there is one other, which runs and prints 110threads there are. And there is one other, which runs and prints
111"async 1", and itself yields the CPU. Since the only other thread 111"async 1", and itself yields the CPU. Since the only other thread
112available is the main program, it continues running and so on. 112available is the main program, it continues running and so on.
113 113
114In more detail, C<async> creates a new thread. All new threads 114In more detail, C<async> creates a new thread. All new threads
115start in a suspended state. To make them run, they need to be put into 115start in a suspended state. To make them run, they need to be put into
255 255
256This method C<down>s the semaphore and returns a so-called guard 256This method C<down>s the semaphore and returns a so-called guard
257object. Nothing happens as long as there are references to it, but when 257object. Nothing happens as long as there are references to it, but when
258all references are gone, for example, when C<costly_function> returns or 258all references are gone, for example, when C<costly_function> returns or
259throws an exception, it will automatically call C<up> on the semaphore, 259throws an exception, it will automatically call C<up> on the semaphore,
260no way to forget it. even when the thread gets C<cancel>ed by another 260no way to forget it. Even when the thread gets C<cancel>ed by another
261thread will the guard object ensure that the lock is freed. 261thread will the guard object ensure that the lock is freed.
262 262
263Apart from L<Coro::Semaphore> and L<Coro::Signal>, there is 263Apart from L<Coro::Semaphore> and L<Coro::Signal>, there is
264also a reader-writer lock (L<Coro::RWLock>) and a semaphore set 264also a reader-writer lock (L<Coro::RWLock>) and a semaphore set
265(L<Coro::SemaphoreSet>). 265(L<Coro::SemaphoreSet>).
307 307
308Both C<get> and C<put> methods can block the current thread: C<get> 308Both C<get> and C<put> methods can block the current thread: C<get>
309first checks whether there I<is> some data available, and if not, it block 309first checks whether there I<is> some data available, and if not, it block
310the current thread until some data arrives. C<put> can also block, as 310the current thread until some data arrives. C<put> can also block, as
311each Channel has a "maximum buffering capacity", i.e. you cannot store 311each Channel has a "maximum buffering capacity", i.e. you cannot store
312more than a specific number of items, which cna be confgiured when the 312more than a specific number of items, which can be configured when the
313Channel gets created. 313Channel gets created.
314 314
315In the above example, C<put> never blocks, as the default capacity is 315In the above example, C<put> never blocks, as the default capacity
316of a Channel is very high. So the foor loop first puts data into the 316of a Channel is very high. So the for loop first puts data into the
317channel, then tries to C<get> the result. Since the async thread hasn't 317channel, then tries to C<get> the result. Since the async thread hasn't
318put anything in there yet (on the firts iteration it hasn't even run 318put anything in there yet (on the firts iteration it hasn't even run
319yet), the result Channel is still empty, so the main thread blocks. 319yet), the result Channel is still empty, so the main thread blocks.
320 320
321Since the only other runnable/ready thread at this point is the squaring 321Since the only other runnable/ready thread at this point is the squaring
332the Coro module itself nor any of its submodules will ever give up the 332the Coro module itself nor any of its submodules will ever give up the
333CPU unless they have to, because they wait for some event to happen. 333CPU unless they have to, because they wait for some event to happen.
334 334
335Be careful, however: when multiple threads put numbers into C<$calculate> 335Be careful, however: when multiple threads put numbers into C<$calculate>
336and read from C<$result>, they won't know which result is theirs. The 336and read from C<$result>, they won't know which result is theirs. The
337solution for this is to ither use a semaphore, or send not just the 337solution for this is to either use a semaphore, or send not just the
338number, but also your own private result channel. 338number, but also your own private result channel.
339 339
340L<Coro::Channel> can buffer some amount of items. It is also very 340L<Coro::Channel> can buffer some amount of items. It is also very
341instructive to read its source code, as it is very simple and uses two 341instructive to read its source code, as it is very simple and uses two
342counting semaphores internally for synchronisation. 342counting semaphores internally for synchronisation.
466 466
467=head1 The Real World - Event Loops 467=head1 The Real World - Event Loops
468 468
469Coro really wants to run in a program using some event loop. In fact, most 469Coro really wants to run in a program using some event loop. In fact, most
470real-world programs using Coro's threads are written in a combination of 470real-world programs using Coro's threads are written in a combination of
471event-based and thread-based techniques, as it is easy to gett he best of 471event-based and thread-based techniques, as it is easy to get the best of
472both worlds with Coro. 472both worlds with Coro.
473 473
474Coro integrates well into any event loop supported by L<AnyEvent>, simply 474Coro integrates well into any event loop supported by L<AnyEvent>, simply
475by C<use>ing L<Coro::AnyEvent>, but can take special advantage of the 475by C<use>ing L<Coro::AnyEvent>, but can take special advantage of the
476L<EV> and L<Event> modules. 476L<EV> and L<Event> modules.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines