… | |
… | |
180 | that will wake up some thread. |
180 | that will wake up some thread. |
181 | |
181 | |
182 | |
182 | |
183 | =head2 Semaphores and other locks |
183 | =head2 Semaphores and other locks |
184 | |
184 | |
185 | Using only C<ready>, C<cede> and C<schedule> to synchronise threads |
185 | Using only C<ready>, C<cede> and C<schedule> to synchronise threads is |
186 | is difficult, especially if many threads are ready at the same |
186 | difficult, especially if many threads are ready at the same time. Coro |
187 | time. Coro supports a number of modules to help synchronising threads |
187 | supports a number of primitives to help synchronising threads in easier |
188 | in easier ways. The first such module is L<Coro::Semaphore>, which |
188 | ways. The first such primitives is L<Coro::Semaphore>, which implements |
189 | implements counting semaphores (binary semaphores are also available as |
189 | counting semaphores (binary semaphores are available as L<Coro::Signal>, |
190 | L<Coro::Signal>): |
190 | and there are L<Coro::SemaphoreSet> and L<Coro::RWLock> primitives as |
|
|
191 | well): |
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 | |
270 | Semaphores are fine, but usually you want to communicate by exchanging |
270 | Semaphores are fine, but usually you want to communicate by exchanging |
271 | data as well. This is where L<Coro::Channel> comes in useful: Channels |
271 | data as well. This is where L<Coro::Channel> comes in useful: Channels |
272 | are the Coro equivalent of a unix pipe (and very similar to amiga message |
272 | are the Coro equivalent of a unix pipe (and very similar to amiga message |
273 | ports) - you can put stuff into it on one side, and read data from it on |
273 | ports :) - you can put stuff into it on one side, and read data from it on |
274 | the other. |
274 | the other. |
275 | |
275 | |
276 | Here is a simple example that creates a thread and sends it |
276 | Here is a simple example that creates a thread and sends it |
277 | numbers. The thread calculates the square of the number, and puts it |
277 | numbers. The thread calculates the square of the number, and puts it |
278 | into another channel, which the main thread reads the result from: |
278 | into 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 { |
… | |
… | |
335 | Be careful, however: when multiple threads put numbers into C<$calculate> |
334 | Be careful, however: when multiple threads put numbers into C<$calculate> |
336 | and read from C<$result>, they won't know which result is theirs. The |
335 | and read from C<$result>, they won't know which result is theirs. The |
337 | solution for this is to either use a semaphore, or send not just the |
336 | solution for this is to either use a semaphore, or send not just the |
338 | number, but also your own private result channel. |
337 | number, but also your own private result channel. |
339 | |
338 | |
340 | L<Coro::Channel> can buffer some amount of items. It is also very |
339 | L<Coro::Channel> can buffer some amount of items. |
341 | instructive to read its source code, as it is very simple and uses two |
|
|
342 | counting 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 | |
347 | What, exactly, constitutes a thread? Obviously it contains the current |
344 | What, exactly, constitutes a thread? Obviously it contains the current |
… | |
… | |
531 | In fact, nontrivial programs follow this pattern even with Coro, so a Coro |
528 | In fact, nontrivial programs follow this pattern even with Coro, so a Coro |
532 | program that uses EV usually looks like this: |
529 | program 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 | |