http://www.perlmonks.org?node_id=1095820


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;