--- Coro/Coro.pm 2009/05/29 07:01:18 1.253 +++ Coro/Coro.pm 2009/06/17 21:38:23 1.256 @@ -340,9 +340,9 @@ does, and are useful when you want to localise some resource to a specific coro. -They slow down coro switching considerably for coros that use -them (But coro switching is still reasonably fast if the handlers are -fast). +They slow down thread switching considerably for coros that use them +(about 40% for a BLOCK with a single assignment, so thread switching is +still reasonably fast if the handlers are fast). These functions are best understood by an example: The following function will change the current timezone to "Antarctica/South_Pole", which @@ -376,6 +376,36 @@ working directory etc.) to a block, despite the existance of other coros. +Another interesting example implements time-sliced multitasking using +interval timers (this could obviously be optimised, but does the job): + + # "timeslice" the given block + sub timeslice(&) { + use Time::HiRes (); + + Coro::on_enter { + # on entering the thread, we set an VTALRM handler to cede + $SIG{VTALRM} = sub { cede }; + # and then start the interval timer + Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0.01, 0.01; + }; + Coro::on_leave { + # on leaving the thread, we stop the interval timer again + Time::HiRes::setitimer &Time::HiRes::ITIMER_VIRTUAL, 0, 0; + }; + + &{+shift}; + } + + # use like this: + timeslice { + # The following is an endless loop that would normally + # monopolise the process. Since it runs in a timesliced + # environment, it will regularly cede to other threads. + while () { } + }; + + =item killall Kills/terminates/cancels all coros except the currently running one.