Is there a way to do something when the Perl thread shuts down?

Say I have 10 threads running at the same time. Is there a way to call a method when the thread ends? I was thinking of something like:

$thread->onFinish(sub { print "I'm done"; });
+5
source share
2 answers

The question you ask in the title and the one you ask in the body are different.

The standard way for another thread is to find out if the thread is working, either wait for it or poll it with is_runningand / or is_joinabledepending on your specific needs.

If all you need to do is print i'm done, make sure that this is the last statement executed in the body of the stream and will be printed.

threads->create(sub {
    # call the actual routine that does the work
    print "i'm finished\n";
});
+5

, : . , onFinish . , ( ).

+1

All Articles