in reply to Re^2: using many threads and conserving stack size
in thread using many threads and conserving stack size
Sorry. I shouldn't post just before leaving. You're right. There is still no architected way to find out how many detached threads are still running. So, you need to implement some counting mechanism yourself.
A simple shared scalar that you increment when the threads start and decrment when the threads end is simple and efficient.
#!/usr/bin/perl -w use strict; use threads; use threads::shared; my $running :shared = 0; for (my $i = 0; $i < 50; $i++){ my $thr = threads->create( \&_run_thread, $i ); $thr->detach; sleep 1; } sleep 1 while $running; print "ok.\n"; sub _run_thread { { lock $running; ++$running; } my ($id) = @_; print "starting thread $id ...\n"; sleep 4; print "exiting thread $id.\n"; { lock $running; --$running; } return; }
|
|---|