Posted on 2010-07-31 16:38:17.354361-07 by jdhedden in response to 12858
Re: block until one thread is joinable?
You asked: As a different request, I noticed that it would be nice to register (at thread create time) a callback function that is invoked to handle the result (in parent context) when join is called.

This can easily be done by creating a hash to register callbacks based on thread IDs:
my %callbacks; my $thr = threads->create(...); $callback{$thr->tid()} = \&callback;
Then wherever you monitor the thread termination queue, you get the ID of the completed thread and invoke the callback:
my $id = $q->dequeue(); $callback{$id}->($id);
The callback then joins with the thread and completes the work:
sub callback { my $id = $_[0]; my $thr = threads->object($id); my $result = $thr->join(); ... }
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.