in reply to Threading and join/termination question
Technically, you cannot set a thread limit with semaphores this way. It does work in practice, but there's no real guarantee that a thread, or all threads, aren't preempted right after the $sem->up().
Second point, do not detach a thread if you need to wait upon it. Grab its end result (return value) with a join(), instead.
You may not want to create too many threads (especially with fine-grained jobs). An alternative that might be suitable is to arrange for a conveyor belt with N workers. Use Thread::Queue for that. Here's a simple, if overly recursive, example:
use threads; use Thread::Queue; my $WQ = Thread::Queue->new('unit001' .. 'unit123'); sub qrun { map { $_ ? (worker($_), qrun()) : () } $WQ->dequeue_nb; } sub worker { warn "Work $_"; select(undef, undef, undef, rand 3); # sleep some return "$_!"; } print for map { $_->join } map { threads->new(\&qrun) } 1 .. 15;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Threading and join/termination question
by locked_user sundialsvc4 (Abbot) on Aug 01, 2014 at 02:51 UTC | |
by SimonPratt (Friar) on Aug 01, 2014 at 10:27 UTC | |
by cormanaz (Deacon) on Aug 01, 2014 at 12:54 UTC | |
by SuicideJunkie (Vicar) on Aug 01, 2014 at 15:16 UTC | |
|
Re^2: Threading and join/termination question
by cormanaz (Deacon) on Aug 01, 2014 at 12:52 UTC | |
by locked_user sundialsvc4 (Abbot) on Aug 01, 2014 at 18:30 UTC |